Domanda

Supponiamo di avere la seguente stringa:

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

e voglio dividere la stringa usando " ass " sequenza di caratteri.

Ho usato:

asd.split("ass");

Non funziona. Cosa devo fare?

È stato utile?

Soluzione

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

Stampe:

this is test 
 this is test

In Java 6. Quale output ti aspettavi?

Altri suggerimenti

Sembra funzionare bene per 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 + "'");
        }
    }
}

Risultato:

'this is test '
' this is test'

Il tuo vero delimitatore è forse diverso? Non dimenticare che split usa il suo parametro come espressione regolare ...

String asd = "this is test foo this is test";
String[] parts = asd.split("foo");

Prova questo funzionerà

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top