[C#]判断字符串中是否包含中文

关键代码:

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

单元测试:

        [TestMethod()]
        public void HasChineseTest()
        {
            string _chineseStr1 = "你好Word";
            bool _expected1 = true;
            bool _actual1 = StringToolV2.HasChinese(_chineseStr1);
            Assert.AreEqual(_expected1, _actual1);

            string _chineseStr2 = "Hello World";
            bool _expected2 = false;
            bool _actual2 = StringToolV2.HasChinese(_chineseStr2);
            Assert.AreEqual(_expected2, _actual2);
        }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }测试结果:

[C#]判断字符串中是否包含中文

上一篇:python机器学习实战(二)


下一篇:php字符串查找函数 php查找字符串中出现的次数函数substr_count,判断字符串中是否包含另一个字符串函数strpos