场景
字符串类似如下
现在要获取S:后面的数字9
实现
indedOf方法,获取某字符所在下标。
flightResult.getCabinString().indexOf(flightResult.getDefaultCabin().getCabinCode())
相当于
“abcd”.indexOf("b")
如果是第25位则返回下标为24
然后使用substring进行截取,从上面的下标开始一直截取到最后
String cabinNum = flightResult.getCabinString().substring(cabin);
通过charAt获取第三个字符
char cabinNum2 = cabinNum.charAt(2);
完整示例代码
for (FlightResult flightResult : book.getFlightResult()) {
if (flightResult.getCabinString().contains(flightResult.getDefaultCabin().getCabinCode())) {
if(flightResult.getCabinString().substring(flightResult.getCabinString().indexOf(flightResult.getDefaultCabin().getCabinCode())).indexOf(2)!=0) {
int cabin = flightResult.getCabinString().indexOf(flightResult.getDefaultCabin().getCabinCode());
String cabinNum = flightResult.getCabinString().substring(cabin);
char cabinNum2 = cabinNum.charAt(2);
if(cabinNum2=='A') {
//10以及以上用A代替
}
else if(Integer.valueOf(cabinNum2)<BookSeatNum) {
CabinEnough=0;
}
cabinInfo="1";
break;
}
}
}
标签:Java,charAt,indexOf,flightResult,substring,getCabinCode,cabinNum2,getCabinString From: https://blog.51cto.com/BADAOLIUMANGQZ/6127844