简体   繁体   English

C#:如何仅从字符串中返回第一组大写字母单词?

[英]C#: How can I only return the first set of capital letter words from a string?

If I wanted to parse back a string to only return the first all capital words in it how would I do that? 如果我想解析一个字符串,仅返回其中的所有大写字母,我该怎么做?

Example: 例:

"OTHER COMMENTS These are other comments that would be here. Some more
comments"

I want to just return "OTHER COMMENTS" 我只想返回"OTHER COMMENTS"

  • These first upper case words can be many and the exact count is unknown. 这些第一个大写单词可能很多,确切的数目未知。
  • There could be other words in the string after with all caps that I just want to ignore. 字符串中可能有其他单词,但我只想忽略所有大写字母。

You can use a combination of Split (to break the sentence into words), SkipWhile (to skip words that aren't all caps), ToUpper (to test the word against it's upper-case counterpart), and TakeWhile (to take all sequential upper-case words once one is found). 您可以组合使用Split (将句子分解为单词), SkipWhile (跳过并非全部大写的单词), ToUpper (针对单词的大写字母对单词进行测试)和TakeWhile (将所有顺序找到一个大写单词)。 Finally, these words can be re-joined using Join : 最后,可以使用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()));

You can loop through the string as an array of chars. 您可以将字符串作为字符数组循环遍历。 To check if the char is uppercase, use Char.IsUpper https://www.dotnetperls.com/char-islower . 要检查char是否为大写,请使用Char.IsUpper https://www.dotnetperls.com/char-islower So, in the loop you can say if its a char - set a flag that we started reading the set. 因此,在循环中您可以说它是否为字符-设置一个标志,我们开始读取该标志。 Then add that char to a collection of chars. 然后将该字符添加到字符集合中。 Keep looping and once it is no longer an upper case char and the flag is still true, break out of the loop. 继续循环,一旦它不再是大写char并且该标志仍然为true,就退出循环。 Then return the collection of chars as a string. 然后将char的集合作为字符串返回。

Hope that helps. 希望能有所帮助。

var input = "OTHER COMMENTS These are other comments that would be here. Some more comments";
var output = String.Join(" ", input.Split(' ').TakeWhile(w => w.ToUpper() == w));

Split it into words, then take words while the uppercase version of the word is the same as the word. 将其拆分为单词,然后在单词的大写形式与单词相同的情况下接受单词。 Then combine them back with the space separator. 然后将它们与空格分隔符组合在一起。

You could also use Regex : 您也可以使用Regex

using System.Text.RegularExpressions;
...
// The Regex pattern is any number of capitalized letter followed by a non-word character.
// You may have to adjust this a bit.
Regex r = new Regex(@"([A-Z]+\W)+"); 
string s = "OTHER COMMENTS These are other comments that would be here. Some more comments";
MatchCollection m = r.Matches(s);
// Only return the first match if there are any matches.
if (m.Count > 0)
{
    Console.WriteLine(r.Matches(s)[0]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM