简体   繁体   中英

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"

  • 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). Finally, these words can be re-joined using 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 . 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. Then return the collection of chars as a string.

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 :

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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