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" , "big" , "string", etc. So I try
myString.split("*");
returns java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*
is a special character in regex so I try escaping....
myString.split("\\*");
same exception. I figured someone would know a quick solution. Thanks.
Use the re. split() method to split a string on punctuation marks, e.g. my_list = re. split('[,.!?]
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
The split() method of the String class accepts a String value representing the delimiter and splits into an array of tokens (words), treating the string between the occurrence of two delimiters as one token. For example, if you pass single space “ ” as a delimiter to this method and try to split a String.
split(String regex) method splits this string around matches of the given regular expression. This method works in the same way as invoking the method i.e split(String regex, int limit) with the given expression and a limit argument of zero. Therefore, trailing empty strings are not included in the resulting array.
split("\\*")
works with me.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With