例如我有一个字符串“13 + 12 + 25 + 55 + 65” 我想在最后一次出现+符号之前找到出现的索引。
subString = str.substring(beginning index,str.lastIndexOf("+") - 1 ));
起始索引将是最后一次出现索引之前的出现索引。
答案 0 :(得分:0)
您应该使用indexOf
两次。
int lastOccurence = str.lastIndexOf('+'); // This will give you last occurence of + in your string
if (lastOccurence > -1) {
int secondLastOccurence = str.lastIndexOf('+', lastOccurence -1);
}
答案 1 :(得分:0)
你想要循环搜索,只要你还没有找到最后一个匹配项就继续循环。
public static int lastIndexOf(char character, String string) {
int index = -1;
do {
int nextIndex = string.indexOf(character, index);
if(nextIndex == -1) return index;
index = nextIndex;
while(true);
}
答案 2 :(得分:0)
你需要分开然后替换空格然后转换成整数,
String division = yourString.split(" +");
Int Indiceprelast = Math.max(0,division.length() - 2);
String preLast = division [Indiceprelast] .replace("","");
Int prelastInt = Integer.parseInt(prelast);