C#字符串中怎么截取中文
1个回答
2017-05-05
展开全部
/// <summary>
/// 传入一个字符串,将字符串中的汉字转换为GBK编码,然后输出
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ConvertChinestStringtoGBKString(string str)
{
string tmp = string.Empty;
try
{
char[] uil = str.ToArray();
string[] con = new string[uil.Length];
for (int j = 0; j < con.Length; j++)
{
con[j] = uil[j].ToString();
if (CheckStringChineseReg(con[j]))
{
con[j] = ConvertChineseToGBK(uil[j].ToString());
}
}
foreach (string c in con)
{
tmp += c.ToString();
}
}
catch (Exception ex)
{
tmp = str;
}
return tmp;
}
/// <summary>
/// 判断字符串中字符是否为汉字
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static bool CheckStringChineseReg(string text)
{
bool res = false;
try
{
char[] c = text.ToArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
{
res = true;
break;
}
}
}
catch (Exception ex)
{
res = true;
}
return res;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询