使用C#判断字符串中是否包含中文字符
使用正则表达式可以快速的判断字符串中是否有中文。
代码示例:
string test1 = "asdasdas121312/*-";string test2 = "阿三大苏打";string test3 = "asda阿三大苏打__132";private void Start(){Debug.Log(HasChinese(test1));Debug.Log(HasChinese(test2));Debug.Log(HasChinese(test3));}/// <summary>/// 判断字符串中是否包含中文/// </summary>/// <param name="str">需要判断的字符串</param>/// <returns>判断结果</returns>public bool HasChinese(string str){return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");}
输出:
核心代码:
public bool HasChinese(string str)
{return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
}