Home > Enterprise >  How to remove invisible [ZWSP] from string in Java
How to remove invisible [ZWSP] from string in Java

Time:07-08

I have a String(assume str) received from some DB query. str = " Aa​ Bk​ Bo​ Ac​ Lc​"; But if copied the same string to intelliJ, It shows the invisible chars in str

enter image description here

I have to split this String (i.e.str) to String[] and then to List. And getting this[ZWSP] in splatted Array and in converted List as well. Also tried few/following techniques to trim and remove this, but did not worked.

        String str = " Aa​ Bk​ Bo​ Ac​ Lc​";
        String[] strArr = str.split("\\ ");

        List<String> splitStrList = Arrays.stream(str.split("\\ "))
                .map(String::trim)
                .collect(Collectors.toList());

---Approach 2

        String[] array2 = Arrays.stream(strArr).map(String::trim).toArray(String[]::new);
       
        String[] trimmedArray = new String[array2.length];

        for (int i = 0; i < array2.length; i  ) {
            trimmedArray[i] = array2[i].trim();
        }
        List<String> trimmedArrayList = Arrays.asList(trimmedArray);

Also few other approach, but while copying the output to intelliJ IDE seeing those [ZWSP] special chars. enter image description here That is creating issue in further processing.

How Can be these spcl chars i.e [ZWSP] removed to get List/Array like [, Aa​, Bk​, Bo​, Ac​, Lc​]

Will Appreciate all suggestions/solutions to this problem.

CodePudding user response:

That character it's called zero-width space as @Rogue mentions. You could use unicode character to remove it:

str.replace("\u200B", "");

Or you could split the string like:

str.split("\\ \u200B");

And then process the array as you need.

See: https://www.fileformat.info/info/unicode/char/200b/index.htm

CodePudding user response:

As mentioned by fellow users I tried the mentioned solution

String str = " Aa​ Bk​ Bo​ Ac​ Lc​";
    String[] strArr = str.split("\u200B");

out put : [ Aa, Bk, Bo, Ac, Lc]

and

 String str = " Aa​ Bk​ Bo​ Ac​ Lc​";
    String[] strArr = str.split("\\ \u200B");
    System.out.println(Arrays.asList(strArr));

out put : [ Aa​ Bk​ Bo​ Ac​ Lc​]

  • Related