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;
   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();
  } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章