Is there like "firstIndexOf" code?
Asked Answered
M

2

8
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
Mcvay answered 23/9, 2017 at 13:49 Comment(0)
B
5

use indexOf

String xStr = "hello/master/yoda";
String remv_last = xStr.substring(xStr.indexOf("/") + 1);
 System.out.println(remv_last);
Barayon answered 23/9, 2017 at 13:56 Comment(0)
C
0

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

Cass answered 23/9, 2017 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.