C#截取指定长度中英文字符串方法 (修改)

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;
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]);
}
else
{
break;
}
}
if(sb.ToString() != stringToSub)
{
sb.Append("...");
}
return sb.ToString();
} 

你可能感兴趣的:(指定长度)