Oracle INSTR在Java程序中具有4个参数?
在Oracle中: 案例1:Oracle INSTR在Java程序中具有4个参数?,java,java-stored-procedures,Java,Java Stored Procedures,在Oracle中: 案例1: SELECT INSTR('Viveok Srinivoasamoorthy','o',15,1) FROM DUAL; 产出:19 案例2: 从DUAL中选择INSTR'Viveok Srinivoasamoorthy',o',15,2; 产出:20 同样,我需要开发一个java程序,其中包含4个参数String、substring、start_position和nthoccurrence 这是我尝试过的代码,但在下面的代码中,我无法找到第n次出现的情况: pu
SELECT INSTR('Viveok Srinivoasamoorthy','o',15,1) FROM DUAL;
产出:19
案例2:
从DUAL中选择INSTR'Viveok Srinivoasamoorthy',o',15,2;
产出:20
同样,我需要开发一个java程序,其中包含4个参数String、substring、start_position和nthoccurrence
这是我尝试过的代码,但在下面的代码中,我无法找到第n次出现的情况:
public static int nthoccur(String str1,String str2,int occurs )
{
int f_occurance=0;
f_occurance=str1.indexOf(str2, occurs-1)+1;
System.out.println("f_occurance Value------*** "+f_occurance);
return f_occurance;
}
public static void main(String args[])
{
int resultinst=nthoccur("Viveok Srinivoasamoorthy","o",15);
System.out.println(resultinst);
}
输出:
f_occurance Value------*** 19
19
现在我想用java程序从字符串的第15个位置找到第2个匹配的o。如何使用Java程序实现案例2?这里有一个应该模仿的方法 评论将帮助您逐步了解其工作原理:
public static int instr(final String str, final String substring, int position, final int occurrence) {
// matches counter
int count = 0;
// index of last match
int indexFound = 0;
// while we haven't reached the desired match count, and we still find another match
while (count != occurrence && (indexFound = str.indexOf(substring, position)) != -1)
{
// increment match count
count++;
// position the next search index, to the end of the current match
position = indexFound + substring.length();
}
if (count == occurrence) {
// the number of occurrences was matched, return the last match index
return indexFound + 1; // index in Java starts at 0
} else {
// the number of occurrences was not matched, return 0
return 0;
}
}
还有一种方法:-1表示不发生。 getInstringString输入、字符串substr、int开始、int出现{ char[]tempstring=input.substringstart.replaceAllsubstr,-.tocharray; int-occurindex=0; int计数器=0; 对于int i=0;i