I've encountered a problem in Java, specific to the String.split function. I'm porting my C# code to Java, and something bothers Java. My C# code is as follows :
foreach (char sep in separators)
if (text.Contains(sep.ToString()))
array = text.Reverse().Split(sep);
Reverse()
is an extension I've made myself which just reverses a string.
separators
is a char array that contains a few separators :
char[] separators = { '&', '!', '#', '?', '%' };
Now, in Java, my code is as follows :
for (char sep : separators) {
String sepp = String.valueOf(sep);
if (text.contains(sepp))
array = new StringBuilder(text).reverse().toString().split(sepp);
}
The problem with this code is that, when I have the specific separator ?
(it's randomly choosed), then it throws me this error (Java only) :
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
at java.base/java.util.regex.Pattern.error(Pattern.java:2027)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2202)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2068)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1782)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1428)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at java.base/java.lang.String.split(String.java:2317)
at java.base/java.lang.String.split(String.java:2364)
at com.anerruption.encryption.Main.decrypt(Main.java:58)
at com.anerruption.encryption.Main.main(Main.java:43)
My guess is that the function string.Split
in C# doesn't use Regex, but the one in Java does. From what I've heard, I need to escape the character. Doing \\?
did not work. How could I do this?
Any help is greatly appreciated!