Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for Capital Letter in String

Tags:

java

I am trying to search a string for the last index of a capital letter. I don't mind using regular expressions, but I'm not too familiar with them.

int searchPattern = searchString.lastIndexOf(" ");      
String resultingString = searchString.substring(searchPattern + 1);

As you can see, with my current code I'm looking for the last space that is included in a string. I need to change this to search for last capital letter.

like image 755
Jay Lefler Avatar asked Nov 28 '22 07:11

Jay Lefler


1 Answers

You can write a method as follows:

public int lastIndexOfUCL(String str) {        
    for(int i=str.length()-1; i>=0; i--) {
        if(Character.isUpperCase(str.charAt(i))) {
            return i;
        }
    }
    return -1;
}
like image 60
Bhesh Gurung Avatar answered Dec 04 '22 21:12

Bhesh Gurung