Rozdelenie reťazcov Java ()

Metóda Java String split () rozdeľuje reťazec v zadanom regulárnom výraze a vracia pole podreťazcov.

Syntax reťazcovej split()metódy je:

 string.split(String regex, int limit)

Reťazec je tu predmetom Stringtriedy.

split () parametre

Metóda reťazca split()môže mať dva parametre:

  • regex - reťazec je pri tomto regulárnom výraze rozdelený (môžu to byť reťazce)
  • limit (voliteľné) - riadi počet výsledných podreťazcov

Ak limitparameter nie je zadaný, split()vráti všetky možné podreťazce.

split () návratová hodnota

  • vráti pole podreťazcov

Poznámka: Ak je regulárny výraz odovzdaný split()neplatný, split()metóda vyvolá PatternSyntaxExpressionvýnimku.

Príklad 1: split () Bez obmedzenia 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)); ) )

Výkon

 výsledok = (a, b, c, d: e)

Tu sme reťazec rozdelili na ::. Pretože limitparameter nie je odovzdaný, vrátené pole obsahuje všetky podreťazce.

split () S limitným parametrom

  • Ak je limitparameter 0 alebo záporný, split()vráti pole obsahujúce všetky podreťazce.
  • Ak je limitparameter kladný (povedzme n), split()vráti maximum npodreťazcov.

Príklad 2: split () S limitným parametrom

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

Výkon

 výsledok, keď je limit -2 = (a, bc, de, fg, h) výsledok, keď je limit 0 = (a, bc, de, fg, h) výsledok, keď je limit 2 = (a, bc: de: fg: h) výsledok, keď je limit 4 = (a, bc, de, fg: h) výsledok, keď je limit 10 = (a, bc, de, fg, h)

Poznámka: Metóda split () berie ako prvý argument regulárny výraz. Ak potrebujete použiť špeciálne znaky, ako sú: , |, ^, *, +atď, je nutné, aby sa vyhli tieto znaky. Napríklad musíme použiť \+na rozdelenie na +.

Príklad 3: split () na znaku +

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

Výkon

 výsledok = (a, e, f)

Tu, na rozdelenie reťazca na +, sme použili \+. Je to preto, lebo +ide o špeciálny znak (má zvláštny význam v regulárnych výrazoch).

Zaujímavé články...