JAVA-截取字符串两边指定字符
程序员文章站
2022-05-24 12:00:12
...
工具类:
/**
* 工具类
*/
public class Tool {
/**
* 截取两边指定的字符
* @param character
* @param symbol
* @return
*/
public static String trim(String character, String symbol){
//
if(character.length() == 0){
return character;
}
//先截取左边
character = (character.substring(0,1).equals(symbol)) ? character.substring(1) : character;
//截取右边
Integer len = character.length();
character = len >0 && (character.substring(len-1,len).equals(symbol)) ? character.substring(0,len-1) : character;
//返回截取后的
return character;
}
}
调用方式:
public class test {
public static void main(String[] args){
String str = "/index/index";
System.out.println(Tool.trim(str,"/"));
}
}
输出结果:
thanks~