1 / 6
文档名称:

C#截取定长字符串函数.doc

格式:doc   页数:6
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

C#截取定长字符串函数.doc

上传人:zgs35866 2015/6/3 文件大小:0 KB

下载得到文件列表

C#截取定长字符串函数.doc

文档介绍

文档介绍: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 = (0, j) + "...";
break;
}
j++;
}
return str;
}
//方法三:
public string CutStr(string sInString,int iCutLength)
{
if(sInString==null || ==0 || iCutLength<=0)
{
return "";
}

int iCount=("Shift_JIS").GetByteCount(sInString);
if(iCount>iCutLength)
{
int iLength=0;
for(int i=0;i<;i++)
{
int iCharLength=("Shift_JIS").GetByteCount(new char[]{sInString[i]});
iLength += iCharLength;
if(iLength==iCutLength)
{
sInString=(0,i+1);
break;
}
else if(iLength>iCutLength)
{
sInString=(0,i);
break;
}
}
}
return sInString;
}
//方法四:利用正则表达式区分英文、汉字,并截取字符串
public static string CutStr(string stringToSub, int length)
{
Regex regex = new Regex("[\u4e00-\u9fa5]+", piled);
char[] stringChar = ();
StringBuilder sb = new StringBuilder();
int nLength =