Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the last char of a String is a specific char [duplicate]

Tags:

java

I have a funtion in my program that should determine if the last char of a string is a / and if not, add a / at the end, however, it always adds a /, even if the last char is a / Here is the code:

public static String setLastCharSlash(String check) {
    if(check.substring(check.length() - 1) != "/") {
        check = check + "/";
    }
    return check;
}
like image 389
ichttt Avatar asked Oct 17 '16 17:10

ichttt


1 Answers

There's a specific method for this use case:

boolean isSlashLast = "your/string/".endsWith("/");
like image 74
rorschach Avatar answered Sep 23 '22 15:09

rorschach