Remove string after last occurrence of a character

java

Solution

You can use `lastIndexOf()` method for same with

if (null != str && str.length() > 0 )
{
    int endIndex = str.lastIndexOf("/");
    if (endIndex != -1)  
    {
        String newstr = str.substring(0, endIndex); // not forgot to put check if(endIndex != -1)
    }
}  

Problem

In my application, I am appending a string to create path to generate a URL. Now I want to remove that appended string on pressing back button. Suppose this is the string: ``` /String1/String2/String3/String4/String5 ``` Now I want a string like this: ``` /String1/String2/String3/String4/ ``` How can I do this?

Original source

Related problems