Minley
Minley

Reputation: 401

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 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.

Upvotes: 5

Views: 221

Answers (7)

Prince Dholakiya
Prince Dholakiya

Reputation: 3401

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

Upvotes: 4

ratish
ratish

Reputation: 491

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

enter image description here

Upvotes: 1

karan
karan

Reputation: 8853

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

Upvotes: 2

Faisal Naseer
Faisal Naseer

Reputation: 4258

Try this pattern.

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

enter image description here

Upvotes: 0

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

Upvotes: 0

Sweeper
Sweeper

Reputation: 274105

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

Upvotes: 5

vs97
vs97

Reputation: 5859

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.

Upvotes: 0

Related Questions