Java – Get string from between two characters

javaspecial charactersstring

I need to get string from between two characters. I have this

S= "10:21:35 |Manipulation       |Mémoire centrale   |MAJ Registre mémoire"

and it have to return 4 strings each in a variable:

a=10:21:35
b=Manipulation
c=Mémoire centrale
d=MAJ Registre mémoire

Best Answer

There's String#split. Since it accepts a regular expression string, and | is a special character in regular expressions, you'll need to escape it (with a backslash). And since \ is a special character in Java string literals, you'll need to escape it, too, which people sometimes find confusing. So given:

String S = "10:21:35 |Manipulation |Mémoire centrale |MAJ Registre mémoire";

then

String[] parts = S.split("\\|");
int index;
for (index = 0; index < parts.length; ++index) {
    System.out.println(parts[index]);
}

would output

10:21:35 
Manipulation 
Mémoire centrale 
MAJ Registre mémoire

(With the trailing spaces on the first three bits; trim those if necessary.)