Remove string after last occurrence of a character

You can use lastIndexOf() method for same with

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

Leave a Comment