English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Il metodo split() di String in Java divide la stringa alla fine dell'espressione regolare specificata e restituisce un array di sottostringhe.
La sintassi del metodo split() della stringa è:
string.split(String regex, int limit)
Il metodo split() della stringa può accettare due parametri:
regex - La stringa viene divisa in questo espressione regolare (può essere una stringa)
limit (Opzionale) - Specifica il numero di sottostringhe generate
Se non viene passato il parametro limit, split() restituisce tutte le sottostringhe possibili.
Restituisce un array di sottostringhe.
Attenzione:Se l'espressione regolare passata a split() è invalida, il metodo split() lancia l'eccezione PatternSyntaxException.
//Importare l'array per convertire l'array in stringa //Usato per stampare l'array import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a::b::c::d:e"; // Dividi la stringa all'interno di "::" // Memorizza i risultati in un array di stringhe String[] result = vowels.split("::"); //Convertire l'array in stringa e stampare System.out.println("result = " + Arrays.toString(result)); } }
Risultato di output
result = [a, b, c, d:e]
Qui, dividiamo la stringa alla fine di ::. Poiché non è stato passato il parametro limit, l'array restituito contiene tutte le sottostringhe.
Se il parametro limit è 0 o negativo, split() restituisce un array contenente tutte le sottostringhe.
Se il parametro limit è positivo (ad esempio n), split() restituisce il numero massimo di sottostringhe n.
// Importare l'array, convertire l'array in una stringa import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a:bc:de:fg:h"; // Suddividere la stringa alla posizione ":" // limit = -2; L'array contiene tutte le sottostringhe String[] result = vowels.split(":", -2); System.out.println("Il risultato quando limit è -2 = " + Arrays.toString(result)); // limit = 0; L'array contiene tutte le sottostringhe result = vowels.split(":", 0); System.out.println("Il risultato quando limit è 0 = " + Arrays.toString(result)); // limit = 2; L'array può contenere al massimo 2 sottostringhe result = vowels.split(":", 2); System.out.println("Il risultato quando limit è 2 = " + Arrays.toString(result)); // limit = 4; L'array può contenere al massimo 4 sottostringhe result = vowels.split(":", 4); System.out.println("Il risultato quando limit è 4 = " + Arrays.toString(result)); // limit = 10; L'array può contenere al massimo 10 sottostringhe result = vowels.split(":", 10); System.out.println("Il risultato quando limit è 10 = " + Arrays.toString(result)); } }
Risultato di output
Il risultato quando limit è -2 = [a, bc, de, fg, h] Il risultato quando limit è 0 = [a, bc, de, fg, h] Il risultato quando limit è 2 = [a, bc:de:fg:h] Il risultato quando limit è 4 = [a, bc:de:fg:h] Il risultato quando limit è 10 = [a, bc, de, fg, h]
Attenzione: split()Il metodo passinga l'espressione regolare come primo parametro. Se si devono utilizzare caratteri speciali come \, |, ^, *, +, ecc., è necessario esitare questi caratteri. Ad esempio, dobbiamo usare\\+ per separare +.
//Importare l'array per convertire l'array in stringa //Usato per stampare l'array import java.util.Arrays; class Main { public static void main(String[] args) { String vowels = "a+e+f"; //Dividere la stringa alla posizione "+" String[] result = vowels.split("\\+"); //Convertire l'array in stringa e stampare System.out.println("result = " + Arrays.toString(result)); } }
Risultato di output
result = [a, e, f]
Qui, per dividere la stringa in +, abbiamo usato \\+. Questo perché + è un carattere speciale (che ha un significato speciale negli espressioni regolari).