I have a String saved in the database like this:
01/01/2014$%^&02/01/2014
I am using the split()
java method. Here is my code:
String value = database.getValue(); // this returns the value mentioned above
String values[] = value.split("$%^&");
System.out.println("length is = " + values.length);
System.out.println(values[0]);
The Output:
length is = 1
01/01/2014$%^&02/01/2014
What am I doing wrong?
Use Pattern#quote
:
String values[] = value.split(Pattern.quote("$%^&"));
What am I doing wrong?
String#split
takes a regex as its argument. Some characters in your String have special meaning . In your case, you want the String representation, not regex. quote
does that for you.
Alternative solutions:
\\\\
(Escaping regex is done by \\
. But in Java, \\
is written \\\\
) String#replace
that accepts String You can try this too. Here we can escape special characters.
String value = "01/01/2014$%^&02/01/2014";
String values[] = value.split("\\$%\\^&");
System.out.println("length is = " + values.length);
System.out.println(values[0]);
Out put:
length is = 2
01/01/2014
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.