This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Before ES6, string concatenation was done directly with “+”, which can be cumbersome when the data is complex or there are a lot of things to concatenate. After ES6 comes out, use the ‘ ‘string template to do the “+” concatenation format:

`stringxxx... The ${} js code stringxxx... `Copy the code

For example,

let obj = { name: "alice", age: 18, sex: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Copy the code

The advantage of string templates is that you can wrap lines as much as you want, insert variables in the middle using ${XXX}, and recognize JS code in {}

About ES6 string new method

Since the usage is relatively simple, I won’t write an example of how to use it. I’ll just go ahead and implement the encapsulation manually

  1. New string lookup method

★ What to look for

Returns true/false to find if the string contains what you are looking for

String.prototype._includes = function (findStr) {
	return this.indexOf(findStr) == -1 ? false : true;
}
let test = "abcd"._includes("abc");
console.log(test)//true
Copy the code

★startsWith

Returns true/false to check if the string starts with someone

String.prototype._startsWith=function(str){
	return this.slice(0,str.length) == str
}
console.log("abcd"._startsWith("ab"));
Copy the code

★endsWith

Returns true/false at any end

String.prototype._endsWith = function(str){ return this.slice(-str.length) == str } console.log("abcd"._endsWith("ab")); //false console.log("abcd"._endsWith("cd")); //trueCopy the code
  1. String repetition

U repeat (n)

Parameter n indicates that the string is repeated n times

Returns a new string that repeatedly contains n initial strings

String.prototype._repeat = function (n) {
	let self =  this.valueOf();
	let res = "";
	let temp = Number(n) || 0;
	if (temp) {
		for (let i = 0; i < temp; i++) {
			res +=self;
		}
		return res;
	}
}
console.log("abc"._repeat(2));
Copy the code
  1. Padding string

★padStart(the length of the entire string after padding, padding stuff)

Inserts a string before a string, returning the filled string

String.prototype._padStart = function (len, padString) { let str = this; padString = padString.toString(); if (padString.length > len - this.length) { padString = padString.toString().slice(0, len - this.length); while (str.length < len) { str += padString; } // If padString is less than len - this.length: // If padString has no content, then each padding position is filled with Spaces; Else if(padstring.length == 0){while (str.length < len) {STR = "" + STR; }}else{// the padString may need to be repeated // The len-this.length bit should be filled before the padString, While (str.length < len) {STR = padString + STR; } } return str; } console.log("abc"._padStart(6, "122111"));Copy the code

★padEnd(the length of the entire string, the padding)

Insert string after string

String.prototype._padEnd = function (len, padString) {
	let str = this;
	padString = padString.toString();
	if (padString.length > len - this.length) {
		padString = padString.toString().slice(0, len - this.length);
		while (str.length < len) {
			str = str + padString;
		}
	}
	else if (padString.length == 0) {
		while (str.length < len) {
			str += " ";
		}
	} else {
		while (str.length < len) {
			str += padString;
		}
	}
	return str;
}
console.log("abc"._padEnd(6, "122111"));
Copy the code