如何获取字符串的第一个单词。
string test = "hello, world"
I used test.Split(' ').FirstOrDefault()
但它返回“hello,
"
我可以尝试将分隔符添加为逗号,但即使有逗号或空格,我也只需要单词“hello”。我的意思是如果字符串 test =“hello world”那么它也应该返回“hello”
正则表达式就是这样!
尝试以下代码:
string pattern = @"^\w+";
string test = "hello, world";
var firstWord = Regex.Match(test, pattern);
Console.WriteLine(firstWord);
// output
// hello
不要忘记包含名称空间 '系统.文本.正则表达式'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)