HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is there an easier way to find the index of the first letter in a string?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thewayfirstfindletterindextherestringeasier

Problem

I have a function that returns the index of the first alphabet letter found in a string, or -1 if no letter is found. It seems like there should be a way to do this with extension methods instead of looping, but I can't think of one:

int findFirstLetter(string str)
{
 for(int ctr=0;ctr<str.Length;ctr++)
 {
  if (Char.IsLetter(str[ctr]))
  {
   return ctr;
  }
 }
 return -1;
}

Solution

I have no issue with the loop. It is about as fast as it can be. I would recommend renaming the method to match the other IndexOf methods.... something like IndexOfAlphaChar.

There is no reason to worry about this code. Move on to bigger problems.

Context

StackExchange Code Review Q#43756, answer score: 9

Revisions (0)

No revisions yet.