Find the pointer of the first uppercase character

As a C # beginner, currently, to find out the index of the first uppercase character in a string, I figured out a way

var pos = spam.IndexOf(spam.ToCharArray().First(s => String.Equals(s, char.ToUpper(s))));

Functionally, the code works fine, except that I had the discomfort of moving the line twice, once to find the Character, and then the Index. Is it possible to get the index of the first character of UpperCase in a single pass using LINQ?

the equivalent way in C ++ would be something like

std::string::const_iterator itL=find_if(spam.begin(), spam.end(),isupper);

Python equivalent syntax will be

next(i for i,e in enumerate(spam) if e.isupper())
+3
source share
7 answers

Well, if you just want to do this in LINQ, you can try using something like

(from ch in spam.ToArray() where Char.IsUpper(ch) 
         select spam.IndexOf(ch))

If you run it against the line, say

"string spam = "abcdeFgihjklmnopQrstuv";"

the result will be: 5, 16.

.

+6

, @Tigran, , :

from i in Enumerable.Range(0, searchText.Length - 1) 
      where Char.IsUpper(searchText.Chars[i]) select i
+2

, , :

, , . , , :

var objectsWithCharAndIndex = myString.Select((c,i)=>new {Char = c,Index = i});

:

var firstCapitalIndex = objectsWithCharAndIndex.First(o => Char.IsUpper(o.Char)).Index;

var allCapitalIndexes = objectsWithCharAndIndex.Where(o => Char.IsUpper(o.Char)).Select(o=>o.Index);

, char .

Linq, Linq.

+2

LINQ:

int index = 0;
foreach(Char c in myString)
{
   if(Char.IsUpper(c)) break;
   index++;
}

// index now contains the index of the first upper case character

, @Tigran.

+1

:

string s = "0123aBc";
int rslt = -1;
var ch = s.FirstOrDefault(c => char.IsUpper(c));
if (ch != 0)
    rslt = s.IndexOf(ch);

, :

char[] UpperCaseChars = new char[] 
{
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z'
};
int rslt = s.IndexOfAny(UpperCaseChars);

, , , , , .

- :

var match = Regex.Match(s, "\p{Lu}", RegexOptions.None);
int rslt = match.Success ? match.Index : -1;

. -, "[A-Z]".

0

Linq , Linq, .

:

internal static class EnumerableExtensions
{
    public static int IndexOfNth<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate, int skip = 0)
    {
        var index = 0;
        foreach (var value in enumerable)
        {
            if (predicate(value))
            {
                if (skip-- == 0)
                    return index;
            }
            index++;
        }
        return -1;
    }

    public static int IndexOfFirst<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
    {
        return enumerable.IndexOfNth(predicate);
    }
}

, , :

var firstUpper = spam[spam.IndexOfFirst(Char.IsUpper)];

: , , IndexOfFirst -1.

:

var secondUpper = spam[spam.IndexOfNth(Char.IsUpper, 1)];

And it works with any enumerable not just character enumerations, for example

var indexOfFirstManager = employees.IndexOfFirst(employee => employee.IsManager);
0
source
String.Concat(
  str.Select(c => Char.IsUpper(c) ? $" {c}" : $"{c}")
);
0
source

All Articles