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
You can also use string.substring and get the required output as
Like this u can assign for the one you want
Like in other answers I suggest you using split() method to separate your string but remeber to use trim if you won't have spaces after parts, like this:
If the length of each column is varient, use the examples given here with the
split
method.However, if you have a fixed-sized file format
substring
will be a much better option. If you look at the implementation ofsubstring
(Java 5 and above if I recall correctly) - you can see that it has an O(1) to create the new strings, whereassplit
uses a regex which is time consuming.Alternatively, org.apache.commons.lang.StringUtils has about 14 variants of the split() method.
You probably want this:
There's also method
trim()
which removes trailing spaces from the strings.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:then
would output
(With the trailing spaces on the first three bits;
trim
those if necessary.)