空字符的问题

[html] view plain copy
  1. /**  
  2.      * 去除空字符  
  3.      * @param str  
  4.      * @return  
  5.      */  
  6.     public static String convert(String str){  
  7.         byte[] bytes = new byte[str.getBytes().length];  
  8.         int pos = 0;  
  9.         for(byte b:str.getBytes()){  
  10.             if(b!=0){  
  11.                 bytes[pos] = b;  
  12.                 pos++;  
  13.             }  
  14.             System.out.println(new String(bytes));  
  15.         }  
  16.         return new String(bytes);  
  17.     }  


故事结局:问题解决。

故事回顾:空字符的问题,用replaceAll(" ","")是清除空格,不能清除空字符。

另:附 \n,空字符,\0的关系:Qt之解析Json出现空指针问题

\0和\n在C语言中都是转义字符

\0 表示空字符NULL,对应的ASCII码为0,通常用来表示字符串的结束标志;

\n 表示回车换行,对应的ASCII码为10,通常用在printf函数中,输出换行;


https://blog.csdn.net/u014039615/article/details/53184495

相关文章: