首页 > 其他 > 详细

手写Indexof

时间:2019-06-11 09:07:51      阅读:124      评论:0      收藏:0      [点我收藏+]
String.prototype.indexO = function(st){
            // console.log(this.length);
            let str = this;
            var j = 0;
            let reflag;
            for(let i = 0;i< str.length;i++){
                if (str.charAt(i) == st.charAt(0)){
                    // console.log(str.charAt(i))
                    // console.log(st.charAt(0))
                    let re_selft = i;
                    let _self = i;
                    while(j<st.length){
                        if(str.charAt(_self)!= st.charAt(j)){
                            reflag = -1;
                            return reflag;
                        }
                        else{
                            reflag =  re_selft
                        }
                        _self++;
                        j++;
                    }
                }
            }
            return reflag
        }
        console.log(‘QWEasd‘.indexO(‘sd‘));
        console.log(‘QWsdEasd‘.indexO(‘sd‘));
        console.log(‘QWsdEa‘.indexOf(‘sd‘));
        console.log(‘QWsdEaaawfaert‘.indexO(‘Eaca‘));
        console.log(‘QWsdEbawfaert‘.indexO(‘Eaaa‘));
        console.log(‘QWsdEaaafaert‘.indexO(‘Eaaa‘));

  

手写Indexof

原文:https://www.cnblogs.com/qdcnbj/p/11001657.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!