JAVA 字符串去空格

xiaoxiao2021-02-28  98

几种方法: 注意:空白字符不等于空格 1,string.trim() 去掉首尾空格,不只是去掉空格。 缺省是去掉的 ('/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '?', '/u2028', '/u2029', ' ', '?'),25种字符。 想去掉空格要 String.trim(' ') 参考: http://blog.csdn.net/muyu114/article/details/5734295 2,string.replace(" ", "") 去掉首尾,中间的空格 3,string.replaceAll("\\s", "") 可替换大部分空白字符,不限于空格 同样,不只是首尾,也有中间。 \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 这里是正则匹配 4,string.replaceAll(" +",""); 去掉所有空格 示例代码: http://blog.csdn.net/ningchao328/article/details/52786797 参考: http://blog.csdn.net/hmyang314/article/details/37883563 http://www.cnblogs.com/lwlxqlccc/p/6179296.html http://www.runoob.com/java/java-string-replaceall.html
转载请注明原文地址: https://www.6miu.com/read-74398.html

最新回复(0)