The string split() metoda rupe un șir dat în jurul potrivirilor expresiei regulate date. După împărțirea față de expresia regulată dată, această metodă returnează o matrice de șiruri.
Input String: 016-78967 Regular Expression: - Output : {'016', '78967'}> Următoarele sunt cele două variante ale metodei split() în Java:
1. Public String [] split (String regex, int limit)
Parametrii
- regex – o expresie regulată delimitătoare Limită – pragul rezultat
Se intoarce
O matrice de șiruri este calculată prin împărțirea șirului dat.
Excepție aruncată
PatternSyntaxException – dacă sintaxa expresiei regulate furnizate este invalidă.
Parametrul limită poate avea 3 valori
- limit> 0 – Dacă acesta este cazul, atunci modelul va fi aplicat de cel mult limită-1 ori, lungimea matricei rezultată nu va fi mai mare de n, iar ultima intrare a matricei rezultată va conține toate intrările dincolo de ultimul model potrivit. limit <0 – În acest caz, modelul va fi aplicat de cât mai multe ori posibil, iar matricea rezultată poate fi de orice dimensiune. limit = 0 – În acest caz, modelul va fi aplicat de cât mai multe ori posibil, matricea rezultată poate fi de orice dimensiune, iar șirurile goale din urmă vor fi eliminate.
Iată cum funcționează:
Fie șirul care urmează să fie împărțit este - geekss@for@geekss
| Regex | Limită | Rezultat |
|---|---|---|
| @ | 2 | {geekss, for@geekss} |
| @ | 5 | {geekss, pentru, geekss} |
| @ | -2 | {geekss, pentru, geekss} |
| s | 5 | {geek, , @for@geek, , } |
| s | -2 | {geek, , , @for@geek, , } |
| s | 0 | {geek, , @for@geek} |
Următoarele sunt exemplele de coduri Java pentru a demonstra funcționarea split()
Exemplul 1:
Java
// Java program to demonstrate working of split(regex,> // limit) with small limit.> public> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >// Custom input string> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'@'>,>2>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
geekss for@geekss>
Exemplul 2:
Java
// Java program to demonstrate working of split(regex,> // limit) with high limit.> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'@'>,>5>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
geekss for geekss>
Exemplul 3:
Java
// Java program to demonstrate working of split(regex,> // limit) with negative limit.> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'@'>, ->2>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>
constructor pythonIeșire
geekss for geekss>
Exemplul 4:
Java
// Java program to demonstrate working of split(regex,> // limit) with high limit.> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'s'>,>5>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
cum se face upgrade java
>
>Ieșire
geek @for@geek>
Exemplul 5:
Java
// Java program to demonstrate working of split(regex,> // limit) with negative limit.> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'s'>, ->2>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
geek @for@geek>
Exemplul 6:
Java
// Java program to demonstrate working of split(regex,> // limit) with 0 limit.> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'geekss@for@geekss'>;> >String[] arrOfStr = str.split(>'s'>,>0>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
geek @for@geek>
2. public String[] split(String regex)
Această variantă a metodei split ia o expresie regulată ca parametru și întrerupe șirul dat în jurul potrivirilor acestei expresii regulate regex. Aici, limita implicită este 0.
Parametrii
regex – o expresie regulată delimitătoare
Se intoarce
O matrice de șiruri este calculată prin împărțirea șirului dat.
Excepție aruncată
PatternSyntaxException – dacă sintaxa expresiei regulate furnizate este invalidă.
Iată câteva exemple de coduri de lucru:
Exemplul 1:
Java
// Java program to demonstrate working of split()> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str> >=>'techcodeview.com:A Computer Science Portal'>;> >String[] arrOfStr = str.split(>':'>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
techcodeview.com A Computer Science Portal>
Exemplul 2:
Java
// Java program to demonstrate working of split()> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'techcodeview.comforStudents'>;> >String[] arrOfStr = str.split(>'for'>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
Geeks Geeks Students>
Se poate vedea în exemplul de mai sus că modelul/expresia regulată for este aplicată de două ori (deoarece for este prezent de două ori în șirul care urmează să fie împărțit)
Exemplul 3:
Java
// Java program to demonstrate working of split()> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'Geeks for Geeks'>;> >String[] arrOfStr = str.split(>' '>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>
harta javaIeșire
Geeks for Geeks>
Exemplul 4:
Java
// Java program to demonstrate working of split()> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'Geeks.for.Geeks'>;> >String[] arrOfStr> >= str.split(>'[.]'>);>// str.split('.'); will give> >// no output...> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
Geeks for Geeks>
Exemplul 5:
citește fișierul excel în java
Java
// Java program to demonstrate working of split()> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'Geekssss'>;> >String[] arrOfStr = str.split(>'s'>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
Geek>
În exemplul de mai sus, șirurile goale de final nu sunt incluse în tabloul rezultat arrOfStr.
Exemplul 6:
Java
// Java program to demonstrate working of split()> public> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >String str =>'GeeksforforGeeksfor '>;> >String[] arrOfStr = str.split(>'for'>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
Geeks Geeks>
În exemplul de mai sus, spațiile de sfârșit (deci nu șir gol) devin un șir în tabloul rezultat arrOfStr.
Exemplul 7:
Java
// Java program to demonstrate working of split()> // using regular expressions> public> class> GFG {> >public> static> void> main(String args[])> >{> >String str =>'word1, word2 word3@word4?word5.word6'>;> >String[] arrOfStr = str.split(>'[, ?.@]+'>);> >for> (String a : arrOfStr)> >System.out.println(a);> >}> }> |
>
>Ieșire
word1 word2 word3 word4 word5 word6>
În exemplul de mai sus, cuvintele sunt separate ori de câte ori se întâlnește unul dintre caracterele specificate în set.