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;
}
IsUpper(String, Int32) Method. This method is used to check whether the specified string at specified position matches with any uppercase letter or not. If it matches then it returns True otherwise returns False.
How would you check if each word in a string begins with a capital letter? The istitle() function checks if each word is capitalized.
LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.
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])
Try this:
where char.IsUpper(qRes[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With