-1

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!

ShiningLea
  • 132
  • 1
  • 12

2 Answers2

1

you can use this way, by concatenating "\\" to each separator only for split() method

char[] separators = { '&', '!', '#', '?', '%' };

for (char sep : separators) {
    if (text.contains(sep+""))
       array = new StringBuilder(text).reverse().toString().split("\\"+sep);
}
Bashir
  • 2,057
  • 5
  • 19
  • 44
  • Like I said in my question, adding "\\" in a character doesn't work. – ShiningLea Jun 02 '20 at 09:28
  • I saw it, but may be you did it in a wrong way, this 2 solutions worked for me, did you try to copy/paste them? if yes what is the error shown. – Bashir Jun 02 '20 at 09:31
  • I tried with your way, it just shows blank text (I'm doing a simple cipher, but the decrypted text is nothing when I add "\\" in the characters (if I don't it's fine except for the "?" character) – ShiningLea Jun 02 '20 at 09:32
  • yea yea, I am sorry, now it is fixed, actually you should use "\\" ONLY for split method. now it should works, sorry again – Bashir Jun 02 '20 at 09:47
  • For some reason, I've tried this before and it didn't work. Now it works, note that I've made my char array a String array because it was easier to work with. – ShiningLea Jun 02 '20 at 13:24
  • @ShiningLea this because I edited the answer, may be you didn't pay attention. anyway glade to hear that it was solved – Bashir Jun 02 '20 at 13:25
1

Meta Character Description .........................................Escape character

*   zero or more occurrence of the characters        \\*
+   one or more occurrence of the characters         \\+ 
?   zero or only one occurrence of the characters    \\? 
^   Start of the character sequence                  \\^
$   End of the character sequence                    \\$

Example of ?

 public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] separators = { "&", "!", "#", "\\?", "%" };
    String str = "Parth?Prem";
    String str2 = "\\?";
    String[] strArray;

    strArray = str.split(str2);

    System.out.println("Given String : " + str);
    for(int i=0;i<strArray.length;i++) {
        System.out.println("strArray["+i+"] = " + strArray[i]);
    }
    
}

Output:

Given String : Parth?Prem

strArray[0] = Parth

strArray[1] = Prem

Bashir
  • 2,057
  • 5
  • 19
  • 44
prem30488
  • 2,828
  • 2
  • 25
  • 57