Java Code
/** 截取2个字符中间的字符串 */
private void GetMiddleString() {
String str = "BB022220011BB007EBB022220011001100113327EBB000897EBB347E";// 要截取的字符串
String temp;
int startIndex = -1, endIndex = -1;
do {
startIndex = str.indexOf("BB022220011"); // 开始截取的字符位置
endIndex = str.indexOf("7E"); // 最后截取的字符位置
System.out.println("开始截取位置:" + startIndex + ",结束截取位置:" + (endIndex + 2));
if (startIndex < 0)
break;
temp = str.substring(startIndex, endIndex + 2);// 截出来的字符串
System.out.println("截出来的字符串:" + temp);
str = str.substring(temp.length(), str.length());// 剩余字符串
System.out.println("剩余字符串:" + str);
} while (str.length() > 0);
}
输出结果
开始截取位置:0,结束截取位置:17
截出来的字符串:BB022220011BB007E
剩余字符串:BB022220011001100113327EBB000897EBB347E
开始截取位置:0,结束截取位置:24
截出来的字符串:BB022220011001100113327E
剩余字符串:BB000897EBB347E
开始截取位置:-1,结束截取位置:9