Java实现String的IndexOf方法

文章来源:https://blog.csdn.net/xiaocy66/article/details/84934398

方法一

public static int IndexOf(String src, String dst) throws Exception{

   if(null == src || src.length() < 1 || null == dst || dst.length() < 1){
       return -1;
   }

   if(dst.length() > src.length()){
       return -1;
   }
   char[] srcArray = src.toCharArray();
   char[] dstArray = dst.toCharArray();
   int srcLen = srcArray.length;
   int dstLen = dstArray.length;

   for(int i = 0; i < srcLen; i++){

       boolean find = false;
       if(srcArray[i] == dstArray[0] && (i+dstLen <= srcLen)){

           int equalCount = 0;
           for (int j = 0; j < dstLen; j++){
               if(srcArray[i+j] == dstArray[j]){
                   equalCount++;
               }
           }

           if(equalCount == dstLen){
               find = true;
           }
       }

       if(find){
           return i;
       }
   }

   return -1;
}

方法二

 public static int IndexOfStr(String src, String dst) throws Exception{

    if(null == src || src.length() < 1 || null == dst || dst.length() < 1 || src.length() < dst.length()){
        return -1;
    }
if(src.equals(dst)){
return 0;
}

    int srcLen = src.length();
    int dstLen = dst.length();
    char[] srcArray = src.toCharArray();
    char[] dstArray = dst.toCharArray();

    for (int i = 0; i < srcLen; i++){
        if (srcArray[i] == dstArray[0] && (i + dstLen) <= srcLen){
        	 // 这里的substring是new新的String空间来存储
            String tmpS = src.substring(i, i + dstLen);
            if (tmpS.equals(dst)){
                return i;
            }
        }
    }

    return -1;
}

String的源码是如何实现的

// 1、入口
public int indexOf(String str) {
    return indexOf(str, 0);
}

// 2、调用下面的:
public int indexOf(String str, int fromIndex) {
	return indexOf(value, 0, value.length,
	            str.value, 0, str.value.length, fromIndex);
}

// 3、最终调用:
static int indexOf(char[] source, int sourceOffset, int sourceCount,
            char[] target, int targetOffset, int targetCount,
            int fromIndex) {
	if (fromIndex >= sourceCount) {
	    return (targetCount == 0 ? sourceCount : -1);
	}
	if (fromIndex < 0) {
	    fromIndex = 0;
	}
	if (targetCount == 0) {
	    return fromIndex;
	}
	
	char first = target[targetOffset];
	int max = sourceOffset + (sourceCount - targetCount);
	
	for (int i = sourceOffset + fromIndex; i <= max; i++) {
	    /* Look for first character. */
	    if (source[i] != first) {
	        while (++i <= max && source[i] != first);
	    }
	
	    /* Found first character, now look at the rest of v2 */
	    if (i <= max) {
	        int j = i + 1;
	        int end = j + targetCount - 1;
	        for (int k = targetOffset + 1; j < end && source[j]
	                == target[k]; j++, k++);
	
	        if (j == end) {
	            /* Found whole string. */
	            return i - sourceOffset;
	        }
	    }
	}
	return -1;
}

从上面的源码可以看到,String内置的IndexOf方法,是直接对其char[]类型的私有变量value去比较的:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
	......
	......
}

并且String公开的返回char[] 的方法只有toCharArray,所以我们自己实现的话只能是调用这个方法得到字符数组,或者直接通过charAt(int index) 获得当前位置的字符。

而源码实现更精简的地方就是对于原字符串,在for循环里面先通过while循环一直查找到当前字符跟目标字符串的首个字符相等的位置,再开始逐个比较后面的字符是否都相等

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值