lastIndexOf() to find last alphanumeric character
Asked Answered
E

3

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.

Extravagate answered 22/1, 2014 at 12:58 Comment(5)
Traverse the string backwards and stop when you find the first (i.e. last) number or letter.Articulation
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?Extravagate
Posted an answer with an example implementation.Articulation
@Extravagate 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.Selfliquidating
Or rather, Bohemian's answer would give you a one-line solution!Selfliquidating
A
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 ;)

Articulation answered 22/1, 2014 at 13:9 Comment(1)
@Extravagate 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.Articulation
A
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(".(?!$)", "");
Arlettearley answered 22/1, 2014 at 13:8 Comment(2)
What if there are non-alphanumeric characters in the beginning or middle? Then your index logic would be wrong.Selfliquidating
@Selfliquidating 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)Arlettearley
D
-1

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

Disannul answered 22/1, 2014 at 13:17 Comment(2)
OP wants the index of the last alphanumeric characterSelfliquidating
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.Articulation

© 2022 - 2024 — McMap. All rights reserved.