Find first letter in a string Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Find first letter in a string without wasting too much if your time.

The question is published on by Tutorial Guruji team.

My program uses an ID system that requires 1-3 digits before a letter then 1-3 digits following the character. For example: 12a123 or 1b83, etc.

What I’m trying to find out is how to find the first occurrence of a letter in the string so I can store the letter as itโ€™s used for an operation between the digits later.

Thanks ๐Ÿ™‚

Answer

Just loop through the characters and grab the first one in the range upper/lowercase A-Z.

public char getCharacter(final String code)
{
    for (char character : code.toCharArray())
    {
        if (   (character >= 'a' && character <= 'z')
            || (character >= 'A' && character <= 'Z'))
        {
            return character;
        }
    }
    throw new RuntimeException("No character in ID: " + code);
}
We are here to answer your question about Find first letter in a string - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji