Java 如何从字符串中获取子字符串

Java 如何从字符串中获取子字符串,java,string,substring,Java,String,Substring,我有一个xml文件,我从中读取这个字符串 http://localhost:8080/sdpapi/request/10/notes/611/ 我的问题是如何从这个字符串中得到611,它是变量,可以是100000 好吧,这取决于你用什么语言写作。。。以c#为例 string s = @"http://localhost:8080/sdpapi/request/10/notes/611/"; s.SubString(s.LastIndexOf('/')); 或 对于java new File(

我有一个xml文件,我从中读取这个字符串

http://localhost:8080/sdpapi/request/10/notes/611/

我的问题是如何从这个字符串中得到611,它是变量,可以是100000

好吧,这取决于你用什么语言写作。。。以c#为例

string s = @"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));

对于java

new File(s).getName();
拆线

String input = "http://localhost:8080/sdpapi/request/10/notes/611/";

String output = input.split("notes/")[1].split("/")[0];
输出是您需要的值

什么语言

无论如何,在大多数情况下,它的语法如下:

String.substring(begin, length);
。。。其中“begin”是字符串-1中字母的编号。用于从上面的字符串中提取http

substring(0, 4);
如果您总是需要最后两个“/”之间的最后一个字符串,您可以使用索引函数检索斜杠的位置(例如@Liran的答案中所述)

//编辑:在Java中,substring的第二个参数不是length,而是endIndex:

String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));

这取决于您使用的编程语言,但正则表达式在大多数情况下应相同:

/(\d+)\/$/

问题缺乏基本的研究,这很好,但我不认为使用“魔法”值是好的(46)。
/(\d+)\/$/