String trim 坑 对于ascii码为160的去不掉

大家在使用string   的trim去除空格的时候,要注意一个坑呀,对于ascii码为160的去不掉

import java.util.Arrays;

/**
 * Created by bjchengpeng on  2018/7/24.
 */
public class TrimTest {
    public static void main(String[] args) {

        String str = "6216910307514221 333";

        String[] array = str.split("\s+");
        System.out.println(Arrays.toString(array));
        System.out.println(array.length);


        String str1 = str.replaceAll("[\s\u00A0]+", " ");
        String[] array1 = str1.split("\s+");
        System.out.println(array1.length);
        System.out.println(Arrays.toString(array1));

        final char c1 = ' '; //db里的空格
        final char c2 = ' '; //手动输入的空格
        System.out.println((int) c1); //160
        System.out.println((int) c2); //32
    }
}

  

// 需要将ASCII为160的空格转成普通的空格
str = str.replaceAll("[\s\u00A0]+", " ");

参考 1 http://www.songshuiyang.site/2018/01/09/Web%E5%90%8E%E5%8F%B0/Java/ASCII%20160%20%E7%A9%BA%E6%A0%BC/

参考 2  http://love67.net/2017/04/07/trim-nbsp

参考 3 http://wwwcomy.iteye.com/blog/2247365

原文地址:https://www.cnblogs.com/chengpeng15/p/9359882.html