Regex to find first capital letter occurrence in a string
.net-2.0, c#, regex, string
Solution
I'm pretty sure all you need is the regex `A-Z` `\p{Lu}`:
public static class Find
{
// Apparently the regex below works for non-ASCII uppercase
// characters (so, better than A-Z).
static readonly Regex CapitalLetter = new Regex(@"\p{Lu}");
public static int FirstCapitalLetter(string input)
{
Match match = CapitalLetter.Match(input);
// I would go with -1 here, personally.
return match.Success ? match.Index : 0;
}
}
Did you try this?
Problem
I want to find the index of first capital letter occurrence in a string. E.g. - ``` String x = "soHaM"; ``` Index should return 2 for this string. The regex should ignore all other capital letters after the first one is found. If there are no capital letters found then it should return 0. Please help.