在 C# 中返回字符串第一个单词的最佳方法是什么?
基本上,如果字符串是 "hello world"
,我需要获取 "hello"
。
谢谢
最佳答案
你可以试试:
string s = "Hello World";
string firstWord = s.Split(' ').First();
Ohad Schneider's comment 是正确的,所以您可以简单地请求 First()
元素,因为总会有至少一个元素。
有关是使用 First()
还是 FirstOrDefault()
的更多信息,您可以了解更多信息 here
关于c# - C#中的左字符串函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3607310/