软件编程
位置:首页>> 软件编程>> C#编程>> C#实现中英文混合字符串截取的方法

C#实现中英文混合字符串截取的方法

作者:shichen2014  发布时间:2022-07-11 10:02:35 

标签:C#,中英文,字符串

本文实例讲述了C#实现中英文混合字符串截取的方法,是C#字符串操作中非常常用的一个方法。分享给大家供大家参考之用。具体方法如下:

具体功能代码如下:


/// <summary>
/// 截取中英文混合字符串
/// </summary>
/// <param name="text">字符文本</param>
/// <param name="length">截取长度</param>
/// <param name="replacetxt">replacetxt替换的多余字符</param>
/// <returns></returns>
public string GetString(string text, int length, string replacetxt)
{
int strLength = 0;
StringBuilder strb = new StringBuilder();
char[] Temp = text.ToCharArray();
for (int i = 0; i != Temp.Length; i++)
{
 if (strLength >= length) //
 {
  strb.Append(replacetxt);
  break;
 }
 else
 {
  if (((int)Temp[i]) < 255) //大于255的都是汉字或者特殊字符
  {
   strLength++;
  }
  else
  {
   strLength = strLength + 2;
  }
  strb.Append(Temp[i]);
 }
}
return strb.ToString();
}

希望本文所述方法对大家C#程序设计有所帮助。

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com