Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing slash from String in Java

I am trying to remove the last character from a string, if it is a /. I am using a string array, temp[], to store the strings.

Here's my code:

char ch = ' ';
for (int st = 0; st < temp.length; st++)
{
    ch = temp[st].charAt(temp[st].length()-1);
    if (ch == '/')
        temp[st] = temp[st].substring(0, temp[st].length()-1);
    result2.append(temp[st]);
}

but i am getting

StringIndexOutOfBoundsException -1

What am I doing wrong?

like image 935
King Aslan Avatar asked Apr 26 '26 02:04

King Aslan


1 Answers

Remove Last Character if it is / java

str = str.replaceAll("/$", "");
like image 107
aioobe Avatar answered Apr 28 '26 21:04

aioobe