How to check / filter only uppercase words from a string using C #?

How to check / filter only uppercase words from a string using C #? I do not want to use "Char.IsUpper ()", skipping each letter of the word to check the top for the same. Is there any simple code to complete this task? with LINQ etc.?

+3
source share
6 answers

How about this?

string test = "This IS a STRING";
var upperCaseWords = test.Split(' ').Where( w => w == w.ToUpper());

upperCaseWords now contains all uppercase words in a string.

+7
source

You can use regex:

using System.Text.RegularExpressions;

string input = "hello hi HELLO hi HI";
MatchCollection matches = Regex.Matches(" " + input + " ", @" [A-Z]* ");
foreach (Match m in matches)
{
    MessageBox.Show(m.Value);
}

Edit: to handle the case when the first / last word is complete, you can simply add a space to each side of the input line. I updated the above example.

+1
source

: unicode, byte , .

// the TEST string
var result = input
    .Split(' ')
    .Where(w => String.Equals(w, w.ToUpper(), StringComparison.Ordinal));
// TEST
+1
0
source

You want only uppercase words from a string, huh?

Maybe something like this?

var upperCaseWords = from w in Regex.Split(text, @"\s+")
                     where char.IsUpper(w[0])
                     select w;

If you need words that are in all uppercase letters (for example, IT), you can use basically the same approach, only with a different predicate:

var upperCaseWords = from w in Regex.Split(text, @"\s+")
                     where w.All(c => char.IsUpper(c))
                     select w;
0
source

You can do this with Regex: http://oreilly.com/windows/archive/csharp-regular-expressions.html

Find all words where the initial letter is uppercase

string t17 = "This is A Test of Initial Caps";
string p17 = @"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)";
MatchCollection mc17 = Regex.Matches(t17, p17);

Search all words Caps

string t15 = "This IS a Test OF ALL Caps";
string p15 = @"(\b[^\Wa-z0-9_]+\b)";
MatchCollection mc15 = Regex.Matches(t15, p15);
0
source

All Articles