menu_on_top
menu_on_top

Reputation: 2613

Android: Remove digits from string

I have a string with a format like "integer1_text integer2" (ex. "23_home 2" ).

I would like to remove the integer1, or replace it with an empty string (so keep only "home 2").

Integer1 is a number from 0 to 1000 and i just want to remove it.

I have tryed to do something like string=mystring.replaceAll("[^0-9]+"_",""); but is not working correctly.

Any idea please? thanks

Upvotes: 0

Views: 85

Answers (3)

Marvin W
Marvin W

Reputation: 444

There are just a few small mistakes in your regex, try:

mystring.replaceAll("^[0-9]+_", "");

Upvotes: 1

injecteer
injecteer

Reputation: 20699

use:

string.replaceAll( "\d+_(.*)", "$1" ) 

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

you can use indexOf("_");

Returns the index within this string of the first occurrence of the specified character.

and use substringIint startIndex, int endIndex); to extract the String you need, with startIndex the index returned by indexOf

Upvotes: 1

Related Questions