I want to get 4 parts of this string
String string = "10 trillion 896 billion 45 million 56873";
The 4 parts I need are "10 trillion" "896 billion" "45 million" and "56873".
What I did was to remove all spaces and then substring it, but I get confused about the indexes. I saw many questions but could not understand my problem.
Sorry I don't have any code
I couldn't run because I didn't know that was right.
This is a way to get your solution easily.
String filename = "10 trillion 896 billion 45 million 56873";
String regex = " [0-9]";
String[] values = filename.split(regex);
// You can get the value by position -> values[0] ... values[n]
// Use the Foreach loop to get all the values.
for(String subValue: values ){
Log.i(TAG, "Part : "+subValue);
}
You can use this regex:
\d+(?: (?:tri|bi|mi)llion)?
It first matches a bunch of digits \d+
, and then optionally (?:...)?
, we match either trillion, billion, or million (?:tri|bi|mi)llion
.
To use this regex,
Matcher m = Pattern.compile("\\d+(?: (?:tri|bi|mi)llion)?").matcher(string);
while (m.find()) {
System.out.println(m.group());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With