Anonymous
Anonymous

Reputation: 193

Split string in Java to show only sequence of characters

I am trying to split a string like the string below

3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4

to a table of strings which does not have any number or sign.

That means

a[0]=x
a[1]=y
a[2]=x
a[3]=w

I tried this

split("(\\+|\\-|\\d)+\\d*")

but it seems that it does not work.

Upvotes: 4

Views: 928

Answers (6)

Bohemian
Bohemian

Reputation: 425238

This one-liner does it all:

String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");

This also deals with leading/trailing characters so you don't get blank elements at the start of the array (like some other answers)

A test with your string:

public static void main(String[] args) {
    String input = "3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
    String[] letters = input.replaceAll("(^[^a-z]*)|([^a-z]*$)", "").split("[^a-z]+");
    System.out.println(Arrays.toString(letters));
}

Output:

[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]

Notice that there's no leading "blank" element in the array

Upvotes: 1

philippe lhardy
philippe lhardy

Reputation: 3286

remark - and – are not same code, one is just ascii minus other is long ( coded UTF8 e28093 )

public class Test {
    public static void main(String pArgs[])
    {
        String s="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";
        String splitreg="(\\+|\\-|\\d|–)+\\d*";     if ( pArgs.length > 0 )
            {
                splitreg=pArgs[0];
        }
        System.out.println("splitting '" + s + "' with '"  + splitreg + "'"); 
        String[] splitted=s.split(splitreg);
        for (int i=0; i < splitted.length; i++ )
            {
                System.out.println("["+ i + "]" + "=" + splitted[i]);
            }
    }
}

/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java Test

splitting '3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4' with '(\+|\-|\d|–)+\d*'
[0]=
[1]=x
[2]=y
[3]=x
[4]=w
[5]=x
[6]=w
[7]=z
[8]=x
[9]=w
[10]=y
[11]=xw
[12]=x
[13]=x
[14]=x
[15]=w
[16]=z

Upvotes: 0

Pshemo
Pshemo

Reputation: 124275

Is this what you are trying to achieve?

 String data="3x2y3+5x2w3–8x2w3z4+3-2x2w3+9y–4xw–x2x3+8x2w3z4–4";

 //lets replace all unnecessary elements with spaces
 data=data.replaceAll("[-+–\\d]", " ");
 // now string looks like:
 // " x y   x w   x w z     x w   y  xw x x   x w z   "

 // lets remove spaces from start and end
 data=data.trim();
 // data looks like:
 // "x y   x w   x w z     x w   y  xw x x   x w z"

 // and split in places where is at least one space
 String[] arr=data.split("\\s+");

 System.out.println(Arrays.toString(arr));

Output:

[x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213351

Edit: -

If you want xw to be together in your resulting array, then you would need to split your string: -

String[] arr = str.split("[-+\\d]+");

Output: -

[, x, y, x, w, x, w, z, x, w, y, xw, x, x, x, w, z]

You can replace all the unwanted characters with empty string, and split on empty string.

String str = "3x2y3+5x2w3-8x2w3z4+3-2x2w3+9y-4xw-x2x3+8x2w3z4-4";
str = str.replaceAll("[-+\\d]", "");        
String[] arr = str.split("");       
System.out.println(Arrays.toString(arr));

Note that, this will add an empty string as the first element of your array, which you can handle.

Output: -

[, x, y, x, w, x, w, z, x, w, y, x, w, x, x, x, w, z]

Note that - sign in your question is different. You should replace it with the one on your keyboard. Currently it is not matching - sign.

Upvotes: 3

Andrew Clark
Andrew Clark

Reputation: 208595

The following should work:

String[] letters = input.split("[-+\\d]+");

Upvotes: 5

mikeslattery
mikeslattery

Reputation: 4119

String[] letters = input.split("[\\d\\+\\-]+");

Upvotes: 0

Related Questions