如题
当输入"我的家abcd的"和3的时候.输出"我的".
C#代码:
public static string SubString1(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()))
{
sb.Append(stringChar[i]);
nLength += 2;
}
else
{
sb.Append(stringChar[i]);
nLength = nLength + 1;
}
if (nLength > length)
break;
}
return sb.ToString();
}
参考:
http://www.cnblogs.com/focustea/archive/2007/08/20/862764.html
阅读(695) | 评论(0) | 转发(0) |