MacDev
MacDev

Reputation: 25

Find first letter in a string

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 :)

Upvotes: 1

Views: 3587

Answers (5)

Sunil
Sunil

Reputation: 166

This one is on the similar lines as Aniket.

public class ExampleCode 
{
    public static void main(String[] args) 
    {
        String[] input = { "12a234", "1s324" };
        String[] operations = new String[input.length];

        for (int i=0; i < input.length; i++) 
        {
            operations[i] = token.replaceAll("\\d{1,3}([A-z])\\d{1,3}", "$1"));
        }
    }
}

Upvotes: 1

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59960

I can suggest two solutions :

Soution 1

If you are using Java 8 you can use :

String str = "12a123";
char firstCharacter = (char) str.chars()
        .filter(c -> String.valueOf((char) c).matches("[a-zA-Z]"))
        .findFirst()
        .orElseThrow(() -> new Exception("No character exist"));//a

Soution 2

You can use replaceAll like this :

String str = "12a123";
String firstCharacter  = str.replaceAll("\\d{1,3}([A-z])\\d{1,3}", "$1");//a

Upvotes: 3

tommybee
tommybee

Reputation: 2551

Use isLetter method of the Java's Character class.

It looks like;

public class CharacterTest {

    private static Character getFirstCharInString(final String candid)
    {
        int found = 0;

        char [] candids = candid.toCharArray();

        for(found = 0; found < candids.length; found++)
        {
            if(Character.isLetter(candids[found])) break;
        }

        return new Character(candids[found]);
    }

    public static void main(String[] args) {

        String ids = "12a123";
        String ids2 = "1b83";

        System.out.println(getFirstCharInString(ids));
        System.out.println(getFirstCharInString(ids2));
    }
}

Upvotes: 1

Michael
Michael

Reputation: 44150

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);
}

Upvotes: 3

Aniket Sahrawat
Aniket Sahrawat

Reputation: 12937

You can use regex for this task, eg:

System.out.println("12a123".replaceAll("\\d{1,3}([A-z])\\d{1,3}", "$1"));

Breakdown:

  • \d{1,3} matches a digit (equal to [0-9])
  • {1,3} Quantifier — Matches between 1 and 3 times, as many times as possible, giving back as needed (greedy)
  • 1st Capturing Group ([A-z])
    • Match a single character present in the list below [A-z] A-z a single character in the range between A (index 65) and z (index 122) (case sensitive)

Upvotes: 2

Related Questions