C#:我怎样才能从字符串中返回第一组大写字母?

Piz*_*ead 1 c# parsing

如果我想解析一个字符串,只返回其中的第一个所有大写单词,我该怎么做?

例:

"OTHER COMMENTS These are other comments that would be here. Some more
comments"
Run Code Online (Sandbox Code Playgroud)

我想回来 "OTHER COMMENTS"

  • 这些第一个大写单词可以很多,并且确切的计数是未知的.
  • 在我想要忽略的所有大写字母后面的字符串中可能还有其他单词.

Ruf*_*s L 6

你可以结合使用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)

  • 我不知道Split接受0个参数? (2认同)