您的位置:首页 > 其它

截取指定长度的字符串,区分汉字和字符

2009-01-20 15:13 609 查看
using System.Text;

using System.Text.RegularExpressions;

int current = 30;

int location_http = lblContext.Text.IndexOf("http");

int location_img = lblContext.Text.IndexOf("img");

if (location_http < current && location_http > 0)

{

current = location_http;

}

if (location_img < current && location_img > 0)

{

current = location_img;

}

lblContext.Text = GetFirstString(lblContext.Text, current);

public static string GetFirstString(string stringToSub, int length)

{

Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);

char[] stringChar = stringToSub.ToCharArray();

StringBuilder sb = new StringBuilder();

int nLength = 0;

bool isCut = false;

for (int i = 0; i < stringChar.Length; i++)

{

if (regex.IsMatch((stringChar[i]).ToString()))

{

sb.Append(stringChar[i]);

nLength += 2;

}

else

{

sb.Append(stringChar[i]);

nLength = nLength + 1;

}

if (nLength >= length)

{

isCut = true;

break;

}

}

if (isCut)

return sb.ToString() + "..";

else

return sb.ToString();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐