Il metodo Java String split () divide la stringa nella regex specificata e restituisce un array di sottostringhe.
La sintassi del split()
metodo delle stringhe è:
string.split(String regex, int limit)
Qui, la stringa è un oggetto della String
classe.
parametri split ()
Il split()
metodo delle stringhe può accettare due parametri:
- regex - la stringa è divisa in questa regex (può essere stringhe)
- limit (opzionale) - controlla il numero di sottostringhe risultanti
Se il limit
parametro non viene passato, split()
restituisce tutte le possibili sottostringhe.
split () Valore restituito
- restituisce un array di sottostringhe
Nota: se l'espressione regolare passata a split()
non è valida, il split()
metodo genera PatternSyntaxExpression
un'eccezione.
Esempio 1: split () Senza limite Parametro
// importing Arrays to convert array to string // used for printing arrays import java.util.Arrays; class Main ( public static void main(String() args) ( String vowels = "a::b::c::d:e"; // splitting the string at "::" // storing the result in an array of strings String() result = vowels.split("::"); // converting array to string and printing it System.out.println("result = " + Arrays.toString(result)); ) )
Produzione
risultato = (a, b, c, d: e)
Qui, dividiamo la stringa in ::
. Poiché il limit
parametro non viene passato, l'array restituito contiene tutte le sottostringhe.
split () Con parametro limite
- Se il
limit
parametro è 0 o negativo,split()
restituisce un array contenente tutte le sottostringhe. - Se il
limit
parametro è positivo (diciamon
),split()
restituisce il massimo din
sottostringhe.
Esempio 2: split () With limit Parameter
// importing Arrays to convert array to string import java.util.Arrays; class Main ( public static void main(String() args) ( String vowels = "a:bc:de:fg:h"; // splitting array at ":" // limit is -2; array contins all substrings String() result = vowels.split(":", -2); System.out.println("result when limit is -2 = " + Arrays.toString(result)); // limit is 0; array contains all substrings result = vowels.split(":", 0); System.out.println("result when limit is 0 = " + Arrays.toString(result)); // limit is 2; array contains a maximum of 2 substrings result = vowels.split(":", 2); System.out.println("result when limit is 2 = " + Arrays.toString(result)); // limit is 4; array contains a maximum of 4 substrings result = vowels.split(":", 4); System.out.println("result when limit is 4 = " + Arrays.toString(result)); // limit is 10; array contains a maximum of 10 substrings result = vowels.split(":", 10); System.out.println("result when limit is 10 = " + Arrays.toString(result)); ) )
Produzione
risultato quando il limite è -2 = (a, bc, de, fg, h) risultato quando il limite è 0 = (a, bc, de, fg, h) risultato quando il limite è 2 = (a, bc: de: fg: h) risultato quando il limite è 4 = (a, bc, de, fg: h) risultato quando il limite è 10 = (a, bc, de, fg, h)
Nota: il metodo split () accetta regex come primo argomento. Se è necessario utilizzare caratteri speciali quali: ,
|
, ^
, *
, +
ecc, è necessario sfuggire a questi personaggi. Ad esempio, dobbiamo usare \+
per dividere in +
.
Esempio 3: split () al carattere +
// importing Arrays to convert array to string // used for printing arrays import java.util.Arrays; class Main ( public static void main(String() args) ( String vowels = "a+e+f"; // splitting the string at "+" String() result = vowels.split("\+"); // converting array to string and printing it System.out.println("result = " + Arrays.toString(result)); ) )
Produzione
risultato = (a, e, f)
Qui, per dividere una stringa in +
, abbiamo usato \+
. È perché +
è un carattere speciale (ha un significato speciale nelle espressioni regolari).