6

I have a string, and I need to find the last occurrence of any alphanumeric char in this string. Whichever the last alphanumeric character is in the string, I want that index. For

text="Hello World!- "

the output would be the index of 'd'

text="Hello02, "

the output would be the index of '2'. I understand I could do it in a 'brute force' kind of way, checking for every letter and every number and finding the highest index, but I'm sure there's a neater way to do it, but I can't find it.

Iverie
  • 85
  • 1
  • 5
  • 4
    Traverse the string backwards and stop when you find the first (i.e. last) number or letter. – Njol Jan 22 '14 at 12:59
  • How would that help? What I don't understand is what I'm supposed to write in .lastIndexOf() [or .indexOf()] to make it print only the index of a alphanumeric character. How do I make it find a letter or a number, and not special character? – Iverie Jan 22 '14 at 13:05
  • Posted an answer with an example implementation. – Njol Jan 22 '14 at 13:10
  • @Iverie the lastIndexOf can only search for a particular fixed string. It can't search for a regex pattern. You will have to take the approach in Njol's answer. – ADTC Jan 22 '14 at 14:06
  • Or rather, Bohemian's answer would give you a *one-line* solution! – ADTC Jan 22 '14 at 14:23

3 Answers3

13

This will work as expected and it will even work on almost all Unicode characters and numbers:

public static final int lastAlphaNumeric(String s) {
    for (int i = s.length() - 1; i >= 0; i--) {
        char c = s.charAt(i);
        if (Character.isLetter(c) || Character.isDigit(c))
            return i;
    }
    return -1; // no alphanumeric character at all
}

It is also much faster than the other answers ;)

mauris
  • 42,982
  • 15
  • 99
  • 131
Njol
  • 3,271
  • 17
  • 32
  • 4
    @Iverie then please accept the answer (green tick on the left). This will not only give us both some points but will also make the question disappear from the unanswered questions lists. – Njol Jan 22 '14 at 13:16
3

Use regex to do the heavy lifting.

To get the index of the last alphanumeric character (-1 if no alphanumerics in the string):

int index = str.replaceAll("[^a-zA-Z0-9]*$", "").length() - 1;

To get the character itself:

String last = str.replaceAll("[^a-zA-Z0-9]*$", "").replaceAll(".(?!$)", "");
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • What if there are non-alphanumeric characters in the beginning or middle? Then your index logic would be wrong. – ADTC Jan 22 '14 at 14:18
  • @ADTC You are mistaken. This logic works perfectly no matter the string. eg tested with `"foo,bar,"` it gives `6` (which is correct). Also, if there are no alphanumeric is still works - the index will be `-1`. (already fixed `int`, but thanks) – Bohemian Jan 22 '14 at 14:20
-1

convert string into character array and get the length of that array will give you the last index of any string...

umair yasin
  • 159
  • 1
  • 2
  • 16
  • OP wants the index of the last alphanumeric character – ADTC Jan 22 '14 at 14:01
  • 1
    This is equivalent to string.length(), but with a useless array allocation inbetween. It's also not the index of the last character, but the index one after the last character. – Njol Jan 22 '14 at 14:06