ridvan
ridvan

Reputation: 743

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 ?

Upvotes: -1

Views: 105

Answers (4)

Emanuel Moecklin
Emanuel Moecklin

Reputation: 28856

How about this :-)

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

or this:

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

Upvotes: 0

Gary Bak
Gary Bak

Reputation: 4798

Or using substring

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

Upvotes: 0

PLivesey
PLivesey

Reputation: 196

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);

Upvotes: 0

Egor
Egor

Reputation: 40193

For this string the following approach will work:

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

Upvotes: 1

Related Questions