关于wps笔试中实现indexof方法的一种写法。
package com.akon;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
private String input;
public int indexof(String str){
int i=-1;
String regex=".*?"+str;
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(input);
if(m.find()){
i=m.group().length()-str.length();
}
return i;
}
public test(String string){
this.input=string;
}
public static void main(String args[]){
test t=new test("abcdsas23sa");
System.out.print(t.indexof("23"));
}
}
结果:7