gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
C#-判断字符中是否含有汉字的两种方法,并提取汉字
- User
C#-判断字符中是否含有汉字的两种方法,并提取汉字
- Assistant
方法一:
使用正则表达式可以判断字符中是否含有汉字,并提取汉字。可以使用 \p{IsCJKUnifiedIdeographs} 正则表达式来匹配汉字。
using System; using System.Text.RegularExpressions; class Program { static void Main() { string str = "Hello你好!"; bool containsChinese = ContainsChinese(str); Console.WriteLine("Contains Chinese: " + containsChinese); string chineseChars = ExtractChinese(str); Console.WriteLine("Extracted Chinese characters: " + chineseChars); } static bool ContainsChinese(string str) { Regex regex = new Regex(@"\p{IsCJKUnifiedIdeographs}"); return regex.IsMatch(str); } static string ExtractChinese(string str) { Regex regex = new Regex(@"\p{IsCJKUnifiedIdeographs}"); string chineseChars = ""; foreach (Match match in regex.Matches(str)) { chineseChars += match.Value; } return chineseChars; } }