Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the desired character from the variable sized strings?

Tags:

java

string

I need to extract the desired string which attached to the word.

For example

 pot-1_Sam  
 pot-22_Daniel
 pot_444_Jack
 pot_5434_Bill

I need to get the names from the above strings. i.e Sam, Daniel, Jack and Bill. Thing is if I use substring the position keeps on changing due to the length of the number. How to achieve them using REGEX.

Update: Some strings has 2 underscore options like

 pot_US-1_Sam  
 pot_RUS_444_Jack
like image 821
Dinesh Ravi Avatar asked Dec 15 '22 09:12

Dinesh Ravi


1 Answers

Assuming you have a standard set of above formats, It seems you need not to have any regex, you can try using lastIndexOf and substring methods.

 String result = yourString.substring(yourString.lastIndexOf("_")+1, yourString.length());
like image 80
Suresh Atta Avatar answered Dec 16 '22 23:12

Suresh Atta