在JAVA中的最后一个斜杠后删除字符串

Ayb*_*kov 18 java url

我在JAVA中最后一次删除URL后删除所有内容时遇到问题.例如,我有一个URL:

http://stackoverflow.com/questions/ask
Run Code Online (Sandbox Code Playgroud)

我想把它改成:

http://stackoverflow.com/questions/
Run Code Online (Sandbox Code Playgroud)

我该怎么做.

Ruc*_*era 46

你可以试试这个

    String str="http://stackoverflow.com/questions/ask";
    int index=str.lastIndexOf('/');
    System.out.println(str.substring(0,index));
Run Code Online (Sandbox Code Playgroud)


RCR*_*RCR 13

如果你想从uRL获得最后一个值

String str="http://stackoverflow.com/questions/ask";
System.out.println(str.substring(str.lastIndexOf("/")));
Run Code Online (Sandbox Code Playgroud)

结果将是"/问"

如果你想要在最后一次正斜杠之后的价值

String str="http://stackoverflow.com/questions/ask";
System.out.println(str.substring(str.lastIndexOf("/") + 1));
Run Code Online (Sandbox Code Playgroud)

结果将是"问"


Sur*_*tta 6

尝试使用String#lastIndexOf()

返回此字符串中最后一次出现的指定字符的索引。

String result = yourString.subString(0,yourString.lastIndexOf("/"));
Run Code Online (Sandbox Code Playgroud)