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

时间:2022-04-03 12:09:48

I have this xml file from where I'm reading this string,

我有这个xml文件,我正在读这个字符串,

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

My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?

我的问题是如何才能得到611这个变量,例如,这个字符串可以是100000?

4 个解决方案

#1


2  

Split the string

拆分字符串

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

String output = input.split("notes/")[1].split("/")[0];

output is the value you need

输出是您需要的值

#2


0  

What language?

Anyway, in most cases it's a syntax like:

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

String.substring(begin, length);

... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write

...其中'begin'是字符串-1中字母的编号。要从上面的字符串中提取http,你会写

substring(0, 4);

In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of @Liran for example).

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

// EDIT: In Java the second parameter of substring is not length, but endIndex:

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

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

#3


0  

It depends on programming language you use, but Regular Expressions should be the same in most of them:

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

/(\d+)\/$/

#4


0  

well, it depend in what language are you writing... in c# for example

好吧,它取决于你用什么语言写作...例如c#

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

or

Path.GetFileName(s);

for java

new File(s).getName();

#1


2  

Split the string

拆分字符串

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

String output = input.split("notes/")[1].split("/")[0];

output is the value you need

输出是您需要的值

#2


0  

What language?

Anyway, in most cases it's a syntax like:

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

String.substring(begin, length);

... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write

...其中'begin'是字符串-1中字母的编号。要从上面的字符串中提取http,你会写

substring(0, 4);

In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of @Liran for example).

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

// EDIT: In Java the second parameter of substring is not length, but endIndex:

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

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

#3


0  

It depends on programming language you use, but Regular Expressions should be the same in most of them:

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

/(\d+)\/$/

#4


0  

well, it depend in what language are you writing... in c# for example

好吧,它取决于你用什么语言写作...例如c#

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

or

Path.GetFileName(s);

for java

new File(s).getName();