如何拆分字符串
'ABCDEFGHIJK'
我知道字符串的起点和终点
Startid endid
1 2
3 5
6 7
8 11
AS
-----
AB
CDE
FG
HIJK
String s = string.subString(startpoint,endpoint);
stringVar.subString(sPoint,ePoint);
确保定期重置 sPoint 和 ePoint 的值
final String toSplit = "ABCDEFGHIJK";
for(final Split split : splits){
if(null != split){
if(0 <= split.startid && split.startid < toSplit.length() && 0 <= split.endid && split.endid < toSplit.length()){
return toSplit.substring(startid, endid);
}
}
return StringUtils.EMPTY;
}
假设一个包含您的 startid/endid 的类拆分(当然也可以是其他选项):
class Split {
public int startid;
public int endid;
public Split(int startid, int endid){
this.startid = startid;
this.endid = endid;
}
}
根据您想要的表
String s = string.subString(startpoint-1,endpoint-1);
如果您愿意,可以滥用Point类(最好只创建自己的对象),请执行以下操作
String s = "ABCEDEFG..." // you string
// Create a list of your points (start and end)
List<Point> list = new ArrayList<Point>();
// Add your points
list.add(new Point(1,2));
list.add(new Point(3,5)); //etc
// Then do this to get your substring
String[] splits = new String[list.size()]
int i= 0;
for(Point p: list)
{
splits[i] = s.substring(p.getX()-1, p.getY()-1)
i++;
}
public static String getSubString(String myString,int statIndex,int endIndex){
return myString.substring(statIndex,endIndex);
}