Let\'s say have a string...
String myString = \"my*big*string*needs*parsing\";
All I want is to get an split the string into \"my\" , \"bi
http://arunma.com/2007/08/23/javautilregexpatternsyntaxexception-dangling-meta-character-near-index-0/
Should do exactly what you need.
You can also use a StringTokenizer.
StringTokenizer st = new StringTokenizer("my*big*string*needs*parsing", "\*");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
One escape \
will not do the trick in Java 6 on Mac OSX, as \
is reserved for \b \t \n \f \r \'\"
and \\
. What you have seems to work for me:
public static void main(String[] args) {
String myString = "my*big*string*needs*parsing";
String[] a = myString.split("\\*");
for (String b : a) {
System.out.println(b);
}
}
outputs:
my
big
string
needs
parsing
split("\\*")
works with me.
This happens because the split method takes a regular expression, not a plain string.
The '*' character means match the previous character zero or more times, thus it is not valid to specify it on its own.
So it should be escaped, like following
split("\\*")
myString.split("\\*");
is working fine on Java 5. Which JRE do you use.