码迷,mamicode.com
首页 > 其他好文 > 详细

去掉字符串前后空格

时间:2018-03-30 17:23:24      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:ace   ring   mat   遇到   substring   turn   start   string   color   

2018-03-30

  今天遇到一个问题,一个字符串的空白通过trim()去除不掉.

1:使用正则消除.

1 public String replaceBlank(String str){
2           Pattern pt=Pattern.compile("^\\s*|\\s*$","");
3           Matcher mt=pt.matcher(str);
4           str=mt.replaceAll("");
5           return str;
6          }

2:使用endsWith 和 startsWith 判断是否以空白开头 结尾

1 while(str.endsWith(" ")){
2     str = str.subString(0,str.length()-1);
3 }
4 while(str.startsWith(" ")){
5     str = str.subString(1);          
6 }

最后发现都不好使 

去掉字符串前后空格

标签:ace   ring   mat   遇到   substring   turn   start   string   color   

原文地址:https://www.cnblogs.com/lxh8/p/8677158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!