C截取定长字符串函数
更新时间:2023-07-28 02:57:01 阅读量: 实用文档 文档下载
C#截取定长字符串函数
/// <summary>
/// °´×Ö·û´®Êµ¼Ê³¤¶È½ØÈ¡¶¨³¤×Ö·û´Ü
/// </summary>
/// <param name="str">Ô-×Ö·û´®</param>
/// <param name="length">Òª½ØÈ¡µÄ³¤¶È</param>
/// <returns>stringÐÍ×Ö·û´®</returns>
public static string CutStr(string str, int length)
{
int i = 0, j = 0;
foreach (char chr in str)
{
if ((int)chr > 127)
{
i += 2;
}
else
{
i++;
}
if (i > length)
{
str = str.Substring(0, j) + "...";
break;
}
j++;
}
return str;
}
//·½·¨Èý£º
public string CutStr(string sInString,int iCutLength)
{
if(sInString==null || sInString.Length==0 || iCutLength<=0) {
return "";
}
int
iCount=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
if(iCount>iCutLength)
{
int iLength=0;
C#截取定长字符串函数
for(int i=0;i<sInString.Length;i++)
{
int
iCharLength=System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[]{sInString[i]});
iLength += iCharLength;
if(iLength==iCutLength)
{
sInString=sInString.Substring(0,i+1);
break;
}
else if(iLength>iCutLength)
{
sInString=sInString.Substring(0,i);
break;
}
}
}
return sInString;
}
//·½·¨ËÄ£ºÀûÓÃÕýÔò±í´ïʽÇø·ÖÓ¢ÎÄ¡¢ºº×Ö£¬²¢½ØÈ¡×Ö·û´®
public static string CutStr(string stringToSub, int length) {
Regex regex = new Regex("[\u4e00-\u9fa5]+", piled);
char[] stringChar = stringToSub.ToCharArray();
StringBuilder sb = new StringBuilder();
int nLength = 0;
for (int i = 0; i < stringChar.Length; i++)
{
if (regex.IsMatch((stringChar[i]).ToString())) {
nLength += 2;
}
else
{
nLength = nLength + 1;
}
if (nLength <= length)
{
sb.Append(stringChar[i]);
}
C#截取定长字符串函数
else
{
break;
}
}
if (sb.ToString() != stringToSub)
{
sb.Append("...");
}
return sb.ToString();
}
//·½·¨Ò»£º
/// <summary>
/// C#½ØÈ¡¶¨³¤×Ö·û´®º¯Êý
/// </summary>
/// <param name="string1">Ô-×Ö·û´®</param>
/// <param name="Len1">³¤¶È£¨Ô-×Ö·û´®ÖÐÈ«ÊǺº×ÖʱµÄºº×Ö¸öÊý£©</param>
/// <returns>½ØÈ¡ºóµÄ×Ö·û´®£¨ReCutStr£©</returns>
public static string CutStr(string string1, int Len1) {
string ReCutstr = string.Empty;
bool Remark = false;//Èç¹û²»¹»³¤¶È£¬Îª¼Ù
int n = string1.Length;
int TrueLen = 0;
int Pcount = 0; //Ó¢ÎÄ×Ö¼ÆÊý
int Gcount = 0; //ÖÐÎÄ×Ö¼ÆÊý
int Lcount = 0; //³¤¶È¼ÆÊý
char[] bytes = string1.ToCharArray();
foreach (char chrA in bytes)
{
if (Convert.ToInt32(chrA) >= 0 && Convert.ToInt32(chrA) <= 255)
{
Pcount = Pcount + 1;
}
else //Èç¹ûÊÇÖÐÎÄ
{
Gcount = Gcount + 1;
}
Lcount = (Pcount / 2) + Gcount;
C#截取定长字符串函数
TrueLen = Pcount + Gcount;
if (Lcount > Len1)//Èç¹û³¤¶ÈÒѹ»
{
ReCutstr = string1.Substring(0, TrueLen) + "..."; Remark = true;
break;
}
}
if (!Remark)//Èç¹ûÊDz»¹»³¤¶È
{
ReCutstr = string1;
}
return ReCutstr;
}
//·½·¨¶þ£º
public static string CutStr(string input,int count)
{
int tempcount = count;
if (input == null)
{
return string.Empty;
}
if (input.Length > count)
{
try
{
int lencount = 0;
int.TryParse(ContainsNumeral(input,
count).ToString(), out lncount);
count = lencount + count;
if (input.Length > count)
{
return input.Substring(0, count) + "..."; }
else
{
return input;
}
}
C#截取定长字符串函数
catch
{
return input.Substring(0, tempcount) + "..."; }
}
return input;
}
/// <summary>
/// ¹ýÂ˵Ä×Ö·û´®ÖдæÔÚÊý×Öʱ£¬¡°Á½¸öÊý×ÖÔøÒ»¸ö×Ö¡±
/// </summary>
/// <param name="input"></param>
/// <param name="count"></param>
/// <returns></returns>
public static double ContainsNumeral(string input, int count) {
string linput = input.Substring(0, count);
double lcount = 0;
int i = 0;
//1.¼òµ¥ÅжÏÊý×Ö
for (int k = 0; k < 10; k++)
{
if (linput.Contains(k.ToString()))
{
lcount += 0.5;
i++;
}
}
////2.È«ÃæÅжÏÊý×Ö
//for (int j = 0; j < linput.Length; j++)
//{
// for (int k = 0; k < 10; k++)
// {
//
1).Contains(k.ToString()))
// {
// lcount += 0.5;
// i++;
// break;
// }
// }
//}
if (linput.Substring(j,
C#截取定长字符串函数
if (i % 2 != 0) {
lcount += 0.5; } return lcount; }
正在阅读:
C截取定长字符串函数07-28
爬山的启示作文700字07-14
外国文学名著《简·爱》读后感(英文版)03-20
骨与关节感染讲义11-07
组成原理试卷题库(2)03-29
美丽的天际作文600字07-12
慈祥的妈妈作文450字02-04
四年级阅读题04-20
- 教学能力大赛决赛获奖-教学实施报告-(完整图文版)
- 互联网+数据中心行业分析报告
- 2017上海杨浦区高三一模数学试题及答案
- 招商部差旅接待管理制度(4-25)
- 学生游玩安全注意事项
- 学生信息管理系统(文档模板供参考)
- 叉车门架有限元分析及系统设计
- 2014帮助残疾人志愿者服务情况记录
- 叶绿体中色素的提取和分离实验
- 中国食物成分表2020年最新权威完整改进版
- 推动国土资源领域生态文明建设
- 给水管道冲洗和消毒记录
- 计算机软件专业自我评价
- 高中数学必修1-5知识点归纳
- 2018-2022年中国第五代移动通信技术(5G)产业深度分析及发展前景研究报告发展趋势(目录)
- 生产车间巡查制度
- 2018版中国光热发电行业深度研究报告目录
- (通用)2019年中考数学总复习 第一章 第四节 数的开方与二次根式课件
- 2017_2018学年高中语文第二单元第4课说数课件粤教版
- 上市新药Lumateperone(卢美哌隆)合成检索总结报告
- 定长
- 截取
- 字符串
- 函数
- 2016-2017年天津市红桥区高一第二学期期中物理试卷与答案
- 中海油海管管理规定
- 中国地质大学北京成矿规律与预测复习与习题
- 14第十四章 与各相关单位的配合措施
- 英语Ⅱ(1)课程基于网络考核改革试点方案
- 高考满分作文800字
- 服务器负载均衡&冗余&热备设计V0.9
- 贵州大学(本科)各专业培养计划
- 计量器具编号规则
- 合肥市规划局主持召开合肥经济圈规划局长联席会议
- 2016高考 周末综合科测试 第19周 生物 2016年1月10日试题
- 历史趣谈:揭秘猛将张飞并非莽撞人而是精通谋略懂兵法
- 中考数学专题复习(二)圆
- 万科天琴湾2012年度营销策略方案
- 油管道酸洗施工作业指导书
- 教科版二年级语文上册第四单元提升练习题及答案
- 商丘土元养殖可行性报告
- 人力outcome理论部分
- 《建筑电气CAD》课程教学研究与实践
- 心外科病例分析题