您当前的位置: 首页 >  c#
  • 6浏览

    0关注

    193博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

使用C#判断字符串中是否包含中文字符

我寄人间雪满头丶 发布时间:2020-05-09 14:21:18 ,浏览量:6

使用正则表达式可以快速的判断字符串中是否有中文。

代码示例:

    string test1 = "asdasdas121312/*-";
    string test2 = "阿三大苏打";
    string test3 = "asda阿三大苏打__132";

    private void Start()
    {
        Debug.Log(HasChinese(test1));
        Debug.Log(HasChinese(test2));
        Debug.Log(HasChinese(test3));
    }

    /// 
    /// 判断字符串中是否包含中文
    /// 
    /// 需要判断的字符串
    /// 判断结果
    public bool HasChinese(string str)
    {
        return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
    }

输出: 在这里插入图片描述

核心代码:

public bool HasChinese(string str)
{
    return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
}
关注
打赏
1648518768
查看更多评论
立即登录/注册

微信扫码登录

0.0489s