44

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?

Ivar
  • 6,138
  • 12
  • 49
  • 61
Krishna Suthar
  • 3,071
  • 6
  • 31
  • 37
  • possible duplicate of [How to get the string after last comma in java?](http://stackoverflow.com/questions/9515505/how-to-get-the-string-after-last-comma-in-java) – Barett Aug 02 '13 at 17:18
  • i have answered the question .. over [here][1] it is an easy way.. [1]: http://stackoverflow.com/questions/1181969/java-get-last-element-after-split/23075419#23075419 – Zar E Ahmer Apr 15 '14 at 10:10

5 Answers5

70

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)
    }
}  
Matt
  • 74,352
  • 26
  • 153
  • 180
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
49
String whatyouaresearching = myString.substring(0, myString.lastIndexOf("/"))
Alex K
  • 22,315
  • 19
  • 108
  • 236
rsan
  • 1,887
  • 2
  • 17
  • 23
  • 4
    "substring", not subString() :) – N Kaushik Nov 17 '15 at 06:20
  • 3
    @rsan Great answer, since the string whatyouaresearching includes the last "/" it will be more like: `String whatyouaresearching = myString.substring(0, myString.lastIndexOf("/") + 1)` – John Spax Jun 02 '17 at 08:58
7

You can use org.apache.commons.lang3.StringUtils.substringBeforeLast which is null-safe.

From the javadoc:

// The symbol * is used to indicate any input including null.
StringUtils.substringBeforeLast(null, *)      = null
StringUtils.substringBeforeLast("", *)        = ""
StringUtils.substringBeforeLast("abcba", "b") = "abc"
StringUtils.substringBeforeLast("abc", "c")   = "ab"
StringUtils.substringBeforeLast("a", "a")     = ""
StringUtils.substringBeforeLast("a", "z")     = "a"
StringUtils.substringBeforeLast("a", null)    = "a"
StringUtils.substringBeforeLast("a", "")      = "a"

Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
</dependency>
ccpizza
  • 28,968
  • 18
  • 162
  • 169
3

Easiest way is ...

        String path = "http://zareahmer.com/questions/anystring";

        int pos = path.lastIndexOf("/");

        String x =path.substring(pos+1 , path.length()-1);

now x has the value stringAfterlastOccurence

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
0

The third line in Nepster's answer should be

String x =path.substring(pos+1 , path.length());

and not String x =path.substring(pos+1 , path.length()-1); since substring() method takes the end+1 offset as the second parameter.

Drew
  • 24,851
  • 10
  • 43
  • 78
huskygrad
  • 1,257
  • 1
  • 10
  • 21