Reputation: 15664
I have three part string with each part seperated by $
symbol
For example,
String s = "abc$efg$xyz";
Now when I split it using split
method like this:
String values[] = s.split("$");
It values
array contains that entire string as a single element.
But when I use this:
String values[]=s.split("\\$");
It perfectly works what I wanted meaning
now the values
array contains abc
,efg
and xyz
on index 0,1 and 2 respectively.
I was wondering why that first split
didn't work as I used similar split
when splitting on a single white space
using split(" ");
Upvotes: 1
Views: 3302
Reputation: 1102
$ shown in your example is in a regex means end of string used in regular expression in java
it is used for
$ Checks if a line end follows
thus you hava to use \\
Upvotes: 0
Reputation: 21863
Because the character $
is a reserved token used in regular expressions to mark the end of a line. That's why you have to escape it using \\
.
Upvotes: 13
Reputation: 887837
String.split
takes a regular expression.
$
in a regex means the nd of the string, so you need to escape it if you want to match a literal $
character.
Upvotes: 7