tags:

views:

486

answers:

2

Suppose I have following string:

String asd = "this is test ass this is test"

and I want to split the string using "ass" character sequence.

I used:

asd.split("ass");

It doesn't work. What do I need to do?

+7  A: 

It seems to work fine for me:

public class Test
{
    public static void main(String[] args) {
        String asd = "this is test ass this is test";
        String[] bits = asd.split("ass");
        for (String bit : bits) {
            System.out.println("'" + bit + "'");
        }
    }
}

Result:

'this is test '
' this is test'

Is your real delimiter different perhaps? Don't forget that split uses its parameter as a regular expression...

Jon Skeet
A: 
public class Splitter {

    public static void main(final String[] args) {
        final String asd = "this is test ass this is test";
        final String[] parts = asd.split("ass");
        for (final String part : parts) {
            System.out.println(part);
        }
    }
}

Prints:

this is test 
 this is test

Under Java 6. What output were you expecting?

johnstok