Remove “empty” character from String

后端 未结 9 1989
不思量自难忘°
不思量自难忘° 2020-12-14 10:42

I\'m using a framwork which returns malformed Strings with \"empty\" characters from time to time.

\"foobar\" for example is represented by: [,f,o,o,b,a,r]

T

9条回答
  •  难免孤独
    2020-12-14 11:32

    A very simple way to remove the UTF-8 BOM from a string, using substring as Denis Tulskiy suggested. No looping needed. Just checks the first character for the mark and skips it if needed.

    public static String removeUTF8BOM(String s) {
        if (s.startsWith("\uFEFF")) {
            s = s.substring(1);
        }
        return s;
    }
    

    I needed to add this to my code when using the Apache HTTPClient EntityUtil to read from a webserver. The webserver was not sending the blank mark but it was getting pulled in while reading the input stream. Original article can be found here.

提交回复
热议问题