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‘));
原文:https://www.cnblogs.com/qdcnbj/p/11001657.html