0

I am attempting to split the components of a string into an array, so that they can be accessed more easily.

For example: 4+5= should become ['4','+','5','='].

Edit: - I would need consecutive numbers to be joined together, and whitespaces can be ignored. Thanks again!

4
  • 2
    String.split(), dude :) Commented Dec 25, 2012 at 20:19
  • 1
    are your strings always number-operator-number-equals or are there other possible strings? Commented Dec 25, 2012 at 20:20
  • 2
    I just answered a similar question Commented Dec 25, 2012 at 20:21
  • I guess, you need to edit your question, as you need a parsed result and not just array of single characters. Commented Dec 25, 2012 at 20:31

4 Answers 4

2

You can solve it with regex lookaround mechanism.

String str = "10 * 10 - 40 + 100/2 = 110";

//but first lets remove white spaces (it will makes regex easier)
String strWithoutSpaces=str.replaceAll("\\s+", "");

String[] tokens = strWithoutSpaces.split("(?<=[-+*/=])|(?=[-+*/=])");

for (String t:tokens)
    System.out.print(t+",");

Output:

10,*,10,-,40,+,100,/,2,=,110,
Sign up to request clarification or add additional context in comments.

Comments

2

You can use

String str = "4+5=";
String[] tokens = str.split("(?!^)");
for (String s : tokens) {
    System.out.println(s);
}

This will output

4  
+  
5  
=

Comments

0

You could use toCharArray() method

    String s ="4+5=";
    char [] stArr = s.toCharArray();
    for(char ss: stArr){
        System.out.println(ss);
    }

Out put

4
+
5
=

5 Comments

This will break for 45+45=90
Will again break. It now splits on empty string.
@Rohit I tested. Neither toCharArray broke nor empty split one. Please can you explain, why you envisage it would be wrong?
@mtk.. toCharArray will treat 4 and 5 as different characters in case of 45+45=90. OP want them to be together. It returns this array: - ['4', '5', '+', '4', '5'...]
@Rohit Thanks for that. I misinterpreted the question. My bad... Great answer to the other similar question.
0

You could do something like this:

public static void main(String args[]) {
    String str = "45+5-26";
    String strArr[] = new String[str.length()];
    StringBuffer sb = new StringBuffer();
    int cnt = 0;
    for (int i = 0; i < str.length(); i++) {
        char ch = str.charAt(i);
        if (ch != '\0') {
            if (ch == ' ') {
                continue;
            }
            if (ch >= 48 && ch <= 57) {
                sb.append(ch);
                continue;
            } else {
                strArr[cnt++] = sb.toString();
                sb = new StringBuffer();
                // sb.append(ch);
            }
            strArr[cnt++] = ch + "";
            sb = new StringBuffer();
        }
    }
    strArr[cnt++] = sb.toString();
    sb = new StringBuffer();
    System.out.println("strArray: ");
    for (int i = 0; i < strArr.length; i++) {
        if (strArr[i] == null)
            break;
        System.out.println(strArr[i]);
    }
}

If you have only operators as the separator between the numbers this would be more easy to get the string tokens.
You can modify as below if you want the tokens separated by a comma:

    for (int i = 0; i < strArr.length; i++) {
        if (strArr[i] == null)
            break;
          //    System.out.println(strArr[i]);
        if(i!=0)
            sbResult.append(",");
        sbResult.append(strArr[i]);

    }
    System.out.println("sbResult: "+sbResult.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.