
java中截取获取固定字符前或后的字符串
截取固定字符后面的字符串:
public static void main(String[] args) {
String testUrl = "http://ceshi.com/test/12345";
String result = testUrl.substring(testUrl.indexOf("com") + 3);
System.out.println("result = " + result);
}
输出结果:
result = /test/12345
截取固定字符前面的字符串:
public static void main(String[] args) {
String testUrl = "http://ceshi.com/test/12345";
String result = testUrl.substring(0,testUrl.indexOf("test"));
System.out.println("result = " + result);
}
result = http://ceshi.com/