How to separate a String in Java. How to use split ()

Question:

Starting from a String, "123-654321" , what I want is to divide it into two Strings:

string1=123
string2=654321

Answer:

For that, the String#split() method is used in which you define a separator, which in the case of this String is "-" .

String string = "123-654321";
String[] parts = string.split("-");
String part1 = parts[0]; // 123
String part2 = parts[1]; // 654321

The method takes a regular expression as a parameter. If you want to use a textual separator, you must escape the \ ^ $ . | ? * + ( ) [ { characters \ ^ $ . | ? * + ( ) [ { preceding them with a \ .

String[] parts = string.split("\\|"); // Separar por "|"

Or you can use the Pattern#quote() method.

String separador = Pattern.quote("|");
String[] parts = string.split(separador);

Are you having trouble using a character as a separator?

There are "special use" characters called metacharacters ,

Metacharacters are non-alphabetic characters that have special meaning in regular expressions. What are they :

\ , ^ , $ , . , | , ? , * , + , ( , ) , { , } , [

If these are used directly to separate a chain they will not work properly, example:

String cadena = "Hola|Stackoverflow|en|español";
String[] parts = cadena.split("|");              
System.out.println(Arrays.asList(parts));

Incorrect output:

[H, o, l, a, |, S, t, a, c, k, o, v, e, r, f, l, o, w, |, e, n, |, e, s, p, a, ñ, o, l]

these characters must be escaped by preceding the "\" character, example:

String cadena = "Hola|Stackoverflow|en|español";
String[] parts = cadena.split("\\|");              
System.out.println(Arrays.asList(parts));

Correct output:

[Hola, Stackoverflow, en, español]

In order to apply the separator in any of the cases, the following method can be used, in which the string and the separator are defined, if it detects that the separator character is a metacharacter, it applies an escape:

private static String[] separaCaracteres(String cadena, String separator){        
    System.out.println("Separator: " + separator);
    String[] parts = null;   
    if(separator.equals("|")|| separator.equals("\\")||separator.equals(".")||separator.equals("^")||separator.equals("$")
            ||separator.equals("?")||separator.equals("*")||separator.equals("+")||separator.equals("(")||separator.equals(")")
            ||separator.equals("{")||separator.equals("[")){
        //Es metacaracter!
        parts = cadena.split("\\"+separator);       
    }else{
        //No es metacaracter.
        parts = cadena.split(separator);
    }    
    return parts;
}

Example calling the above method:

   System.out.println(Arrays.asList(separaCaracteres("Hola\\Stackoverflow\\en\\español", "\\")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola^Stackoverflow^en^español", "^")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola$Stackoverflow$en$español", "$")).toString());       
   System.out.println(Arrays.asList(separaCaracteres("Hola.Stackoverflow.en.español", ".")).toString());       
   System.out.println(Arrays.asList(separaCaracteres("Hola|Stackoverflow|en|español", "|")).toString());       
   System.out.println(Arrays.asList(separaCaracteres("Hola?Stackoverflow?en?español", "?")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola*Stackoverflow*en*español", "*")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola+Stackoverflow+en+español", "+")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola(Stackoverflow(en(español", "(")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola)Stackoverflow)en)español", ")")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola{Stackoverflow{en{español", "{")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola}Stackoverflow}en}español", "}")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola[Stackoverflow[en[español", "[")).toString());       
   System.out.println(Arrays.asList(separaCaracteres("Hola Stackoverflow en español", " ")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola½Stackoverflow½en½español", "½")).toString());
   System.out.println(Arrays.asList(separaCaracteres("Hola-Stackoverflow-en-español", "-")).toString());  

Exit:

Separator: \
[Hola, Stackoverflow, en, español]
Separator: ^
[Hola, Stackoverflow, en, español]
Separator: $
[Hola, Stackoverflow, en, español]
Separator: .
[Hola, Stackoverflow, en, español]
Separator: |
[Hola, Stackoverflow, en, español]
Separator: ?
[Hola, Stackoverflow, en, español]
Separator: *
[Hola, Stackoverflow, en, español]
Separator: +
[Hola, Stackoverflow, en, español]
Separator: (
[Hola, Stackoverflow, en, español]
Separator: )
[Hola, Stackoverflow, en, español]
Separator: {
[Hola, Stackoverflow, en, español]
Separator: }
[Hola, Stackoverflow, en, español]
Separator: [
[Hola, Stackoverflow, en, español]
Separator:  
[Hola, Stackoverflow, en, español]
Separator: ½
[Hola, Stackoverflow, en, español]
Separator: -
[Hola, Stackoverflow, en, español]
Scroll to Top