比较两字符的大小、相等长度、是否包含

//比较两字符的大小、相等长度、是否包含
function compareLen(str1,str2,n1=0,n2=0) {
  //求出相等部分
  let len=0;
  let dis=-2;
  let isEnd=1;
  while (dis===-2){
    const l1=n1+len
    const l2=n2+len
    if(l1<str1.length&&l2<str2.length&&str1.charCodeAt(l1)===str2.charCodeAt(l2)){
      len++;
    }else if(l1===str1.length&&l2===str2.length){
      dis=0;
    }else if(l1===str1.length){
      dis=-1
    }else if(l2===str2.length){
      dis=1
    }else{
      isEnd=0;
      if(str1.charCodeAt(l1)>str2.charCodeAt(l2)){
        dis=1
      }else{
        dis=-1
      }
    }
  }
  return [dis,len,isEnd]
}

//demo
const arr=compareLen('12345','123')
console.log(arr)

  

原文地址:https://www.cnblogs.com/caoke/p/14678540.html