This is the 26th day of my participation in the August Text Challenge.More challenges in August

String Indicates the description of the object

Strings are a basic data type in JavaScript.

The length property of a String declares the number of characters in the String.

The String class defines a number of methods for manipulating strings, such as extracting or retrieving characters or substrings from a String.

Note that JavaScript strings are immutable, and no method defined by the String class can alter the contents of a String. Methods like String.toupperCase () return an entirely new String, rather than modifying the original String.

New method in ES6

identify

ES6 used indexOf to determine whether a string contains a substring. ES6 added a substring identification method.

methods describe
includes() Returns a Boolean value to determine if the parameter string was found.
startsWith() Returns a Boolean value that determines whether the argument string is at the beginning of the original string
endsWith() Returns a Boolean value that determines whether the argument string is at the end of the original string.
let string = "apple,banana,orange"; 
string.includes("banana"); // true 
string.startsWith("apple"); // true 
string.endsWith("apple"); // false 
string.startsWith("banana",6) // true
Copy the code

Note:

  • These three methods only return booleans, so indexOf and lastIndexOf are used if you need to know the position of the substring.
  • These three methods throw an error if they pass in a regular expression instead of a string. IndexOf and lastIndexOf, on the other hand, convert the regular expression to a string and search for it.

repeat

methods describe
repeat() Returns a new string that repeats the string a specified number of times.
console.log("Hello,".repeat(2));  // "Hello,Hello,"
Copy the code

completion

methods describe
padStart() Returns a new string that completes the original string from the header (left) with the argument string.
padEnd() Returns a new string that completes the original string from the end (right) of the argument string.
console.log("h".padStart(5,"o"));  // "ooooh"
console.log("h".padEnd(5,"o"));    // "hoooo"
console.log("h".padStart(5));      // "    h"
Copy the code

Template string

Template strings are equivalent to enhanced strings, with backquotes. In addition to being regular strings, template strings can be used to define multi-line strings, and variables and expressions can be added to strings.

let name = "Mike"; let age = 27; let info = `My Name is ${name},I am ${age+1} years old next year.` console.log(info); // My Name is Mike,I am 28 years old next year.
Copy the code

The label template

A tag template is a call to a function that takes a template string as an argument.

f`My Name is ${name},I am ${age+1} years old next year.`; / / equivalent to f ([' My Name is', 'I am', 'years old next year.'], 'Mike, 28).Copy the code

other

  • Because ES6 implements the Iterator interface for strings, you can use for… of
  • You can convert arrays using extended operations
[...'asdfasdf']
// ["a", "s", "d", "f", "a", "s", "d", "f"]
Copy the code

Before the es6

String object properties

attribute describe
constructor A reference to the function that created the object
length Length of string
prototype Allows you to add properties and methods to objects

String method

methods describe
anchor() Create an HTML anchor.
big() Display strings in large font.
blink() Displays a flash string.
bold() Display strings in bold.
charAt() Returns the character at the specified position.
charCodeAt() Returns the Unicode encoding of the character at the specified position.
concat() Connection string.
fixed() Displays a string as typewriter text.
fontcolor() Displays the string using the specified color.
fontsize() Displays the string using the specified size.
fromCharCode() Creates a string from the character encoding.
indexOf() Retrieves the string.
italics() Display strings in italics.
lastIndexOf() Searches the string from back to front.
link() Displays the string as a link.
localeCompare() Compare two strings in a locally specific order.
match() Finds a match for one or more regular expressions.
replace() Replaces the substring that matches the regular expression.
search() Retrieves the value that matches the regular expression.
slice() Extracts a fragment of a string and returns the extracted portion in a new string.
small() Use small font size to display strings.
split() Splits a string into an array of strings.
strike() Use a stripper to display a string.
sub() Displays strings as subscripts.
substr() Extracts a specified number of characters from the initial index number in the string.
substring() Extracts the character between two specified index numbers in a string.
sup() Displays strings as superscripts.
toLocaleLowerCase() Converts a string to lowercase.
toLocaleUpperCase() Converts the string to uppercase.
toLowerCase() Converts a string to lowercase.
toUpperCase() Converts the string to uppercase.
toSource() Represents the source code of the object.
toString() Returns a string.
valueOf() Returns the original value of a string object.