How to substring this String

2019-06-28 02:21发布

问题:

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.

回答1:

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));
        }


回答2:

This is a way to get your solution easily.

String filename = "10 trillion 896 billion 45 million 56873";
String regex = " [0-9]";

String[] parts = filename.split(regex);
String part1 = parts[0]; // 10 trillion
String part2 = parts[1]; // 896 billion
String part3 = parts[2]; // 45 million
String part4 = parts[3]; // 56873

You can also get values by using the foreach loop.

for(String str: parts){
    Log.i(TAG, "onCreate: parts : "+str);
}


回答3:

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());
}


回答4:

Regex is the answer

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

final String regex = "(\\d+\\s+\\w+)|\\d+";
final String string = "10 trillion 896 billion 45 million 56873";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 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



回答5:

You can use the following Regex expression:

String string = "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.



回答6:

try the below code.

public static void main(String args[]) { String str = "10 trillion 896 billion 45 million 56873 ";

    String[] strlist = str.split(" ");

    for (int i = 0; i < strlist.length; i++) {
        String data=textNumber(strlist[i], (i+1<strlist.length?strlist[i + 1]:""));
        if (data != null) {
            System.out.println(data);
            i++;
        }
    }

}
public static String textNumber(String str1,String str2){

    if(str1.matches("[0-9]+")){
        if(str2.matches("[a-zA-Z]+")) {
            return  str1 + " " + str2;
        }
        return str1;
    }
     return null;
}


回答7:

Try this pattern.

([^\s]+\s+[^\s]+|[^\s]+)