Skip to content Skip to sidebar Skip to footer

How To Substring This String

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

Solution 1:

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(StringsubValue: values ){
    Log.i(TAG, "Part : "+subValue);
}

Solution 2:

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.

enter image description here

To use this regex,

Matcher m = Pattern.compile("\\d+(?: (?:tri|bi|mi)llion)?").matcher(string);
while (m.find()) {
    System.out.println(m.group());
}

Solution 3:

Below code will work. Check comments for added instructions.

String input = "10 trillion 896 billion 45 million 56873";
        String pattern = "\\s\\d";     // this will match space and number thus will give you start of each number.
        ArrayList<Integer> inds = new ArrayList<Integer>();
        ArrayList<String> strs = new ArrayList<String>();
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(input);
        while (m.find()) {
            inds.add(m.start());          //start will return starting index.
        }

        //iterate over start indexes and each entry in inds array list will be the end index of substring. //start index will be 0 and for subsequent iterations it will be end index + 1th position.int indx = 0;
        for(int i=0; i <= inds.size(); i++) {
            if(i < inds.size()) {
                strs.add(input.substring(indx, inds.get(i)));    
                indx = inds.get(i)+1;
            } else {
                strs.add(input.substring(indx, input.length()));
            }
        }

        for(int i =0; i < strs.size(); i++) {
            System.out.println(strs.get(i));
        }

Solution 4:

Regex is the answer

import java.util.regex.Matcher;
import java.util.regex.Pattern;

finalStringregex="(\\d+\\s+\\w+)|\\d+";
finalStringstring="10 trillion 896 billion 45 million 56873";

finalPatternpattern= Pattern.compile(regex, Pattern.MULTILINE);
finalMatchermatcher= pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (inti=1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

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

enter image description here

Solution 5:

You can use the following Regex expression:

Stringstring = "10 trillion 896 billion 45 million 56873";
String[] array = string.split("(?<!\\G\\w+)\\s");

Essentially, we're splitting on every second space rather than on every space.

Post a Comment for "How To Substring This String"