昨天有个老兄面试遇到这个问题,向我求助,我也无能为力,
哪位同仁可解,小弟在此谢过了...题目:
编写一个方法实现String对象中的IndexOf方法,不能使用String类中的方法,
除了charAt方法。如:有字符串a,字符串B,写一个算法查找A在B中的位置,
如果不存在返回-1
哪位同仁可解,小弟在此谢过了...题目:
编写一个方法实现String对象中的IndexOf方法,不能使用String类中的方法,
除了charAt方法。如:有字符串a,字符串B,写一个算法查找A在B中的位置,
如果不存在返回-1
package cn.d510;import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Demo { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String source = "sdinncduj843409";
System.out.println(Demo.indexOf(source, "du"));;
}
public static int indexOf(String source,String str){
Matcher mat = Pattern.compile(str).matcher(source);
mat.find();
int indexof = -1;
try {
indexof = mat.start();
} catch (Exception e) {
// TODO: handle exception
indexof = -1;
return indexof;
}
return indexof;
}
}
int h=0;
int k=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i)==b.charAt(0)){
h=i;
for(int j=1;j<b.length();j++){
if(a.charAt(i+j)==b.charAt(j)){
k=1;
}
else{
k=0;
break;
}
}
}
}
if(k==0){
return -1;
}
else{
return h;
}
}
public static void main(String[] args){
String a="wrtehjkl";
String b="he";
PD pd=new PD();
int x=pd.IndexOf(a, b);
System.out.println(x);
}
}
.length()也不能用,我的神啊!
/***
* 查找一个字符串在另外一个字符串中的位置
* @param source 原字符串
* @param findstr 要查找的字符串
* @return
*/
public int StringIndexOf(String source, String findstr){
int sourcecount = 0;
int findstrcount = 0;
int indexnum = 0; try{
while(true){
source.charAt(sourcecount++);
}
}catch (Exception e){
sourcecount--;
}
try{
while(true){
findstr.charAt(findstrcount++);
}
}catch (Exception e){
findstrcount--;
}
for(int i=0; i<sourcecount; i++){
if(source.charAt(i)==findstr.charAt(0)){
for(int j=1; j<findstrcount; j++){
if(source.charAt(i+j)!=findstr.charAt(j)){
break;
}
indexnum = i;
}
}
}
return indexnum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TestString ts = new TestString();
int num = ts.StringIndexOf("estse测试123sdf", "123");
System.out.println(num);
}}
呵呵,和我想的一样,用exception代替.length();不过有个bugStringIndexOf("123estse12测试123sdf", "123") == 12,但实际0就已经满足了
哎!实现到实现了,不能得分啊,用了.length()方法
Matcher.getTextLength() line: 1140 [local variables unavailable]
Matcher.reset() line: 291
Matcher.<init>(Pattern, CharSequence) line: 211
Pattern.matcher(CharSequence) line: 888 ---- > Matcher mat = Pattern.compile(str).matcher(source);
他还有个小bug,截止目前无人完成这个变态答案。他是最接近的一个。
/**
* 查找一个字符串在另外一个字符串中的位置
*
* @param source 原字符串
* @param findstr 要查找的字符串
* @return
*/
public int StringIndexOf(String source, String findstr) {
int sourcecount = 0;
int findstrcount = 0;
int indexnum = -1;
boolean flag = false;
try {
while (true) {
source.charAt(sourcecount++);
}
} catch (Exception e) {
sourcecount--;
} try {
while (true) {
findstr.charAt(findstrcount++);
}
} catch (Exception e) {
findstrcount--;
} for (int i = 0; i < sourcecount; i++) {
if (source.charAt(i) == findstr.charAt(0)) {
if(findstrcount == 1){
indexnum = i;
break;
}
for (int j = 1; j < findstrcount; j++) {
if (source.charAt(i + j) != findstr.charAt(j)) {
break;
}
if (j == findstrcount - 1) {
flag = true;
indexnum = i;
break;
}
}
if(flag){
break;
}
}
}
return indexnum;
} /**
* @param args
*/
public static void main(String[] args) {
TT tt = new TT();
int num = tt.StringIndexOf("1223123123123", "123");
System.out.println(num);
}}