简体   繁体   中英

How to get selected String from Text

I try to get only this part " 10.135.57.1/24 " in " 10.135.57.1/24 05492518979 " in android. How can I do this?

I tried below to use substring but it can use for get integer How can I get only 10.135.57.1/24 ?

For this string the following approach will work:

String[] parts = "10.135.57.1/24 05492518979".split(" ");
String partThatYouNeed = parts[0];

In the substring method you're defining which subsection of the String you're after.

If you did the following you'd get your result:

String whatYouWant = "10.135.57.1/24 05492518979".substring(0, 14);

Or using substring

String yourString = "10.135.57.1/24 05492518979";
int index = yourString.indexof(" ");
String partThatYouNeed = yourString.substring(0,index);

How about this :-)

"10.135.57.1/24 05492518979".replaceFirst(" \\d+$", "")

or this:

"10.135.57.1/24 05492518979".replaceFirst(" .+$", "")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM