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.
Regex is the answer
It will print
Full match: 10 trillion
Group 1: 10 trillion
Full match: 896 billion
Group 1: 896 billion
Full match: 45 million
Group 1: 45 million
Full match: 56873
Group 1: null
Below code will work. Check comments for added instructions.
You can also get values by using the foreach loop.
Try this pattern.
try the below code.
public static void main(String args[]) { String str = "10 trillion 896 billion 45 million 56873 ";
You can use the following Regex expression:
Essentially, we're splitting on every second space rather than on every space.