2

I need to get position of the first letter in a string.

Follow a basic example. The first letter is T, So I need to know the position of T, for that I can use indexOf("T").

I would like to get the first letter by regex instead to add the letter hard-coded (T).

Thanks in advance.

public class RegexMatches {
    public static void main(String args[]) {

        String line = "000 1 This is my message";
        int first = line.indexOf("T");
        line = line.substring(first, line.length());

        System.out.println(line);
     }
} 
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Edson Martins
  • 78
  • 1
  • 2
  • 8
  • 6
    What's your problem? – Maroun Apr 30 '14 at 14:19
  • possible duplicate of [Java: method to get position of a match in a String?](http://stackoverflow.com/questions/2615749/java-method-to-get-position-of-a-match-in-a-string) – Robin Apr 30 '14 at 14:20
  • @Robin: But that question doesn't consider regex matches, does it? – Tim Pietzcker Apr 30 '14 at 14:22
  • The indexOf can't receive "T" because it's a example, if the message starts with other letter it will not works. – Edson Martins Apr 30 '14 at 14:23
  • @Mena please undelete your answer. It was basically correct, just don't read the group but the start index. – Thomas Apr 30 '14 at 14:24
  • @TimPietzcker: my bad, should have linked to that one: [Java: method to get position of a match in a String?](http://stackoverflow.com/questions/8938498/get-the-index-of-a-pattern-in-a-string-using-regex) – Robin Apr 30 '14 at 14:25
  • @Thomas: I think Tim's answer is correct. – Bhesh Gurung Apr 30 '14 at 14:25
  • @BheshGurung yes, but Mena's is also correct with the minor change suggested. (And he was first to answer ;) ) – Thomas Apr 30 '14 at 14:27

3 Answers3

8

If, as I interpret, you're looking for the first alphabetic character in a sentence or its index through regular expressions, here's an example:

String line = "000 1 This is my message";
Pattern p = Pattern.compile("\\p{Alpha}");
Matcher m = p.matcher(line);
if (m.find()) {
    System.out.println(m.group());
    System.out.println("At: " + m.start());
}

Output

Found: T
At: 6
Mena
  • 47,782
  • 11
  • 87
  • 106
8

If I read the Java docs correctly, you're looking for the start method of the match object:

String line = "000 1 This is my message";
Pattern p = Pattern.compile("\\p{L}");
Matcher m = p.matcher(line);
if (m.find()) {
    System.out.println(m.start());
}
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
3

If you don't need regex you can do it just by looping through (My reccomendation)

public int findFirstLetterPosition(String input) {
    char c;
    for (int i = 0; i < input.length(); i++) {
        c = input.charAt(i);
        if ( (c >= 'a' && c <= 'z') || (c >= 'A' || c <= 'Z') ) {
            return i;
        }
    }
    return -1; // not found
}

EDIT

In response to some comments, you could also do this less explicitly using a built in static method, and add some handling for non-ASCII characters using isLetter:

public int findFirstLetterPosition(String input) {
    for (int i = 0; i < input.length(); i++) {
        if (Character.isLetter(input.charAt(i))) {
            return i;
        }
    }
    return -1; // not found
}

You can also use isAlphabetic:

public int findFirstLetterPosition(String input) {
    for (int i = 0; i < input.length(); i++) {
        if (Character.isAlphabetic(input.charAt(i))) {
            return i;
        }
    }
    return -1; // not found
}

The latter includes some alphabetic characters from other languages which are iffy on whether to box them as characters or phrases

ref: Character.isLetter, Character.isAlphabetic

Community
  • 1
  • 1
Farmer Joe
  • 6,020
  • 1
  • 30
  • 40