昨天有个老兄面试遇到这个问题,向我求助,我也无能为力,
哪位同仁可解,小弟在此谢过了...题目:
编写一个方法实现String对象中的IndexOf方法,不能使用String类中的方法,
除了charAt方法。如:有字符串a,字符串B,写一个算法查找A在B中的位置,
如果不存在返回-1

解决方案 »

  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;
    }
    }
      

  2.   

    public class PD { public int IndexOf(String a,String b){
    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);

    }
    }
      

  3.   


    .length()也不能用,我的神啊!
      

  4.   

    package jtetris.main;public class TestString {
    /***
     * 查找一个字符串在另外一个字符串中的位置
     * @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);
    }}
      

  5.   


    呵呵,和我想的一样,用exception代替.length();不过有个bugStringIndexOf("123estse12测试123sdf", "123") == 12,但实际0就已经满足了
      

  6.   


    哎!实现到实现了,不能得分啊,用了.length()方法
      

  7.   

    正则也用到了string里面的.length()方法String.length() line: 651 [local variables unavailable]
    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);
      

  8.   


    他还有个小bug,截止目前无人完成这个变态答案。他是最接近的一个。
      

  9.   

    public class TT {
    /**
     * 查找一个字符串在另外一个字符串中的位置
     * 
     * @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);
    }}