Is there like "firstIndexOf" code?

Viewed 11609
String xStr = "hello/master/yoda";
String remv_last = xStr.substring(0, xStr.lastIndexOf("/"))
System.out.println(remv_last);

output

hello/master

My question is; how can I get this output, thanks for helping.

master/yoda
2 Answers

use indexOf

String xStr = "hello/master/yoda";
String remv_last = xStr.substring(xStr.indexOf("/") + 1);
 System.out.println(remv_last);

You can use simple indexOf it will give you the first occurrence only

        String xStr = "hello/master/yoda";
    String remv_last = xStr.substring(0, xStr.lastIndexOf("/"));
    int firstIndex = xStr.indexOf("/");
    String firstCharacter = xStr.substring(firstIndex,xStr.length());
    System.out.println(remv_last);
    System.out.println(firstCharacter);

It will print required

Related