De Java String split () - methode verdeelt de string op de opgegeven regex en retourneert een array van subtekenreeksen.
De syntaxis van de stringmethode split()
is:
string.split(String regex, int limit)
String is hier een object van de String
klasse.
split () Parameters
De stringmethode split()
kan twee parameters aannemen:
- regex - de string is op deze regex verdeeld (kan strings zijn)
- limit (optioneel) - bepaalt het aantal resulterende subtekenreeksen
Als de limit
parameter niet wordt doorgegeven, worden split()
alle mogelijke subtekenreeksen geretourneerd.
split () Retourwaarde
- geeft een reeks subtekenreeksen terug
Opmerking: als de doorgegeven reguliere expressie split()
ongeldig is, geeft de split()
methode een PatternSyntaxExpression
uitzondering.
Voorbeeld 1: split () Zonder limiet Parameter
// 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)); ) )
Uitvoer
resultaat = (a, b, c, d: e)
Hier splitsen we de string op ::
. Omdat de limit
parameter niet wordt doorgegeven, bevat de geretourneerde array alle subtekenreeksen.
split () Met limiet Parameter
- Als de
limit
parameter 0 of negatief is,split()
wordt een array geretourneerd die alle subtekenreeksen bevat. - Als de
limit
parameter positief is (laten we zeggenn
),split()
retourneert het maximum aantaln
subtekenreeksen.
Voorbeeld 2: split () Met limietparameter
// 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)); ) )
Uitvoer
resultaat als limiet -2 = (a, bc, de, fg, h) resultaat als limiet 0 is = (a, bc, de, fg, h) resultaat als limiet 2 = (a, bc: de: fg: h) resultaat als limiet 4 = (a, bc, de, fg: h) resultaat als limiet 10 = (a, bc, de, fg, h)
Opmerking: de methode split () gebruikt regex als het eerste argument. Als u speciale tekens, zoals te gebruiken: ,
|
, ^
, *
, +
enz, moet u deze tekens te ontsnappen. We moeten bijvoorbeeld gebruiken \+
om te splitsen op +
.
Voorbeeld 3: split () op het + -teken
// 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)); ) )
Uitvoer
resultaat = (a, e, f)
Hier hebben we, om een string te splitsen +
, gebruikt \+
. Het is omdat het +
een speciaal teken is (heeft een speciale betekenis in reguliere expressies).