Decoupling assignment of strings (detailed explanation)

One of the great advantages of an excellent person is: perseverance in the face of disadvantages and difficulties. -Beethoven

Determine whether the current string contains another given substring

ES5 whether the method comprising providing a
string.indexOf (searchStr) Method
* action - Returns the index containing the specified value specified string matching substring
* Results -
* comprising - Returns the index matching values
* not Contains-returns -1

console.log(str.indexOf('we', 3)); //5 //从指定位置开始往后查找
console.log(str.indexOf('we'));//1 //从整个字符串中查找

ES6 provides whether to include the method
includes () method

console.log(str.includes('we')); //true//从整个字符串中查找
console.log(str.includes('we', 3)); //true//从指定位置开始往后查找

Determine whether the current string "ends" with another given substring

endswidth() method

  • Determine whether the end of the string with the index value as the termination point, the return value is Boolean
let str = 'swedrwefweda';
console.log(str.endsWith('da')); //true
console.log(str.endsWith('swe', 3)); //true

Determine whether the current string is "given" by another given substring

startswidth() method

  • Does not mean that the specified string starts with another string
  • Indicates whether the specified index value of the specified string starts with another string
  • Judge whether the string starts with the specified index value. If yes, return true, otherwise false
let str = 'swedrwefweda';
console.log(str.startsWith('sw')); //true
console.log(str.startsWith('dr', 3)); //false

Insert picture description here

The above methods are all case-sensitive.
Because you are very likely to encounter in your work you don’t want her to be case-sensitive, so I wrote an example (make it case-insensitive)
so that you can work Efficiency a little bit faster

let str = 'swedrwefweda';
Object.defineProperty(String.prototype, 'myIncludes', {
    value: function (searchStr, index) {
        let str = this.toLowerCase();
        searchStr = searchStr.toLowerCase();
        if (typeof index === 'number') {
            return str.includes(searchStr, index);
        } else {
            return str.includes(searchStr);
        }
    }
});
console.log(str.myIncludes('we', 3))

I hope you can accumulate a little bit of progress in your work

Today's golden sentence

If you don't accumulate steps, you can't reach a thousand miles; if you don't accumulate small currents, you can't become a river.

Guess you like

Origin blog.csdn.net/weixin_50001396/article/details/111556297