如果我想解析一个字符串,只返回其中的第一个所有大写单词,我该怎么做?
例:
"OTHER COMMENTS These are other comments that would be here. Some more
comments"
Run Code Online (Sandbox Code Playgroud)
我想回来 "OTHER COMMENTS"
你可以结合使用Split(将句子分解为单词),SkipWhile(跳过不是全部大写ToUpper的单词),(测试单词不是大写的对应单词),和TakeWhile(取所有连续的大写单词)一旦找到一个).最后,可以使用以下方法重新连接这些单词Join:
string words = "OTHER COMMENTS These are other comments that would be here. " +
"Some more comments";
string capitalWords = string.Join(" ", words
.Split()
.SkipWhile(word => word != word.ToUpper())
.TakeWhile(word => word == word.ToUpper()));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |