首页 > 代码库 > 去除String首尾字符

去除String首尾字符

参考: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首尾字符