Dumbo
Dumbo

Reputation: 14112

How to check if a string starts with a capital letter in a LINQ query

I have the following code, I am trying to get the strings which starts with capital, but I don't know how! without linq I can do it but inside LINQ... no idea!

string[] queryValues1 = new string[10] {"zero", "one", "two", "three", "four", "five", "six", "seven","nine", "ten" };
string[] queryValues2 = new string[3] { "A", "b", "c" };

var queryResult =
    from qResult in queryValues1
    from qRes in queryValues2
    where qResult.Length > 3
    where qResult.Length < 5
    where qRes[0].StartWithCapital //how to check if qRes started with a capital letter?
    select qResult + "\t" + qRes + Environment.NewLine;

foreach (var qResult in queryResult)
{
    textBox1.Text += qResult;
}

Upvotes: 8

Views: 15694

Answers (6)

neeraj lohumi
neeraj lohumi

Reputation: 1

   static void Main(string[] args)
    {
        Console.Write("\nLINQ : Find the uppercase words in a string : ");
        Console.Write("\n----------------------------------------------\n");

        string strNew;

        Console.Write("Input the string : ");
        strNew = Console.ReadLine();
        //string[] newStr = strNew.Split(' ');
        var ucWord = WordFilt(strNew);
        Console.Write("\nThe UPPER CASE words are :\n ");
        foreach (string strRet in ucWord)
        {
            Console.WriteLine(strRet);
        }
        Console.ReadLine();

    }

  static IEnumerable<string> WordFilt(string mystr)
    {
        var upWord =  mystr.Split(' ')
        .Where(x=> !string.IsNullOrEmpty(x) && char.IsUpper(x[0]));
     return upWord;

    }

Upvotes: 0

Stilgar
Stilgar

Reputation: 23561

where Char.IsUpper(qRes.FirstOrdefault())

It is the same as outside LINQ.

Upvotes: 2

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20620

The earlier solutions here all assume queryValues2 consists of strings with at least one character in them. While that is true of the example code, it isn't necessarily always true.

Suppose, instead, you have this:

string[] queryValues2 = new string[5] { "A", "b", "c", "", null };

(which might be the case if the string array is passed in by a caller, for example).

A solution that goes straight for qRes[0] will raise an IndexOutOfRangeException on the "" and a NullReferenceException on the null.

Therefore, a safer alternative for the general case would be to use this:

where !string.IsNullOrEmpty(qRes) && char.IsUpper(qRes[0])

Upvotes: 11

Aman
Aman

Reputation: 548

try something like this (in this code arr is a string[]):

from a in arr
                    where ((int)a.ToCharArray()[0] >= 65 && (int)a.ToCharArray()[0] <= 90)
                    select a

The gist is to check whether the first character's ASCII value lies in the range of capital letters or not.

Upvotes: 0

SLaks
SLaks

Reputation: 887479

Check Char.IsUpper(qRes[0]).

Upvotes: 3

slugster
slugster

Reputation: 49984

Try this:

where char.IsUpper(qRes[0])

Upvotes: 6

Related Questions