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

去除String首尾字符

时间:2017-06-23 19:25:09      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:art   substring   last   内容   bool   turn   begin   indexof   bsp   

参考:http://blog.csdn.net/csdnbenbenchong/article/details/7667199

 

内容:

 1 /**
 2  * 字符串工具类.
 3  * @author sunruyi
 4  */
 5 public class StringUtil {
 6     /**
 7      * 去除字符串首尾出现的某个字符.
 8      * @param source 源字符串.
 9      * @param element 需要去除的字符.
10      * @return String.
11      */
12     public static String trimFirstAndLastChar(String source,char element){
13         boolean beginIndexFlag = true;
14         boolean endIndexFlag = true;
15         do{
16             int beginIndex = source.indexOf(element) == 0 ? 1 : 0;
17             int endIndex = source.lastIndexOf(element) + 1 == source.length() ? source.lastIndexOf(element) : source.length();
18             source = source.substring(beginIndex, endIndex);
19             beginIndexFlag = (source.indexOf(element) == 0);
20             endIndexFlag = (source.lastIndexOf(element) + 1 == source.length());
21         } while (beginIndexFlag || endIndexFlag);
22         return source;
23     }
24 }

 

去除String首尾字符

标签:art   substring   last   内容   bool   turn   begin   indexof   bsp   

原文地址:http://www.cnblogs.com/tangyongathuse/p/7071273.html

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