String creation

1.new String()

let stringObj = new String("123")
console.log(stringObj) // object
Copy the code

This is a string object, try not to do!!!!

2. Create basic string values

let stringStr = "123"
console.log(stringStr) // string
Copy the code

String methods

1. Find the method

a)charAt(index)

Function: Returns a character at a specified position.

Note: The subscript of the first character in the string is 0. If the index argument is not between 0 and String.length, the method returns an empty string.

let str = "1123"
console.log(str.charAt(0)) / / "1"
console.log(str.charAt(5)) / / ""
Copy the code

b)charCodeAt(index)

Function: Returns the Unicode encoding of the character at the specified position.

Note: The subscript of the first character in the string is 0. If index is negative, or greater than or equal to the length of the string, charCodeAt() returns NaN.

let str = "1123"
console.log(str.charCodeAt(0)) / / 49
console.log(str.charCodeAt(5)) // NaN
console.log(str.charCodeAt(5)) // NaN
Copy the code

c)formCharCode(Unicode,Unicode,Unicode)

Creates a string using Unicode encoding.

Parameters: One or more Unicode values, the Unicode encoding of the characters in the string to be created.

Note: This method is static on String, and each character in the String is specified by a separate numeric Unicode encoding. It cannot be used as a method on a String that you have created. Grammar so it should be a String. FromCharCode (), rather than myStringObject. FromCharCode (). This method returns a String instead of a String object.

console.log(String.fromCharCode(189.43.190.61)); / / "1/2 level + delighted many customers ="
String.fromCharCode(65.66.67);   // "ABC"
String.fromCharCode(0x2014);       / / "-"
String.fromCharCode(0x12014);      / / "-"; The number 1 is dropped and ignored
String.fromCharCode(8212);         / / "-"; 8212 is the decimal representation of 0x2014
Copy the code

2. Location method

A) indexOf (searchValue, fromIndex)

The indexOf() method returns the first occurrence of a specified string value in a string. Returns -1 if no matching string is found.

Note: The indexOf() method is case sensitive. Searchvalue required. Specifies the string value to retrieve. FromIndex An optional integer. Specifies the starting point in a string for retrieval. It has valid values from 0 to String Object.length – 1. If omitted, the retrieval begins with the first character of the string.

Grammar: string. IndexOf (searchvalue, start)

let str="Hello world, welcome to Beijing.";
console.log(str.indexOf("welcome")); / / 13
Copy the code

B) the lastIndexOf (searchValue, fromIndex)

Function: The lastIndexOf() method returns the last position of a specified string value or, if the second argument start is specified, searches the specified position in a string from back to front.

Note: This method retrieves the string backwards, but returns the last occurrence of the substring calculated from the starting position (0). See if it contains strings.

The location to start the search is at start of the string or at the end of the string (when no start is specified).

Returns -1 if no matching string is found

The lastIndexOf() method is case sensitive!

let str="I am from Runoob, welcome to Runoob site.";
console.log(str.lastIndexOf("runoob")); / / 28
Copy the code

3. Matching method

a)match(regexp)

Function: A method retrieval returns the result of a string matching a regular expression.

Parameter :regexp A regular expression object. If a non-regular expression object is passed in, it is implicitly converted to a RegExp using New RegExp(obj). If you give no arguments and use match() directly, you will get an Array: [“”] containing an empty string.

Return value: If the G flag is used, all results that match the full regular expression are returned, but the capture group is not returned. If the G flag is not used, only the first full match and its associated capture group (Array) are returned. In this case, the returned item will have the additional properties described below.

let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
let regexp = /[A-E]/gi;

console.log(str.match(regexp));// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
console.log(str.match());   / / / ""
Copy the code

b)replace(regexp|substr, newSubStr|function)

The replace() method returns a new string that replaces some or all of the pattern matches with the replacement value. The pattern can be a string or a regular expression, and the replacement value can be a string or a callback function that is called on every match. If pattern is a string, only the first match is replaced.

Parameters:

Regexp (Pattern) A regexp object or its literal. What the re matches is replaced by the return value of the second argument.

Substr (pattern) A string to be replaced by newSubStr. It is treated as an entire string, not as a regular expression. Only the first match is replaced.

NewSubStr (replacement) is used to replace a string that matches the first argument in the original string. Special variable names can be interpolated into the string. See using strings as arguments below.

Function (replacement) A function that creates a new substring and returns a value that replaces the result of the first argument. Refer to specifying a function as an argument below.

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  return [p1, p2, p3].join(The '-');
}
let newString = 'abc12345#$*%'.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);  // abc - 12345 - #$*%

let re = /apples/gi;
let str = "Apples are round, and apples are juicy.";
let newstr = str.replace(re, "oranges");
console.log(newstr);// oranges are round, and oranges are juicy.
Copy the code

c)str.split([separator[, limit]])

The split() method splits a String into an array of substrings using the specified delimiter String, with a specified split String determining the location of each split.

Parameters:

Separator specifies a string representing the point at which each split should occur. Separator can be a string or regular expression. If the plain text delimiter contains more than one character, the entire string must be found to represent the split point. If the delimiter is omitted or not present in STR, the returned array contains an element consisting of the entire string. If the delimiter is an empty string, the array of each character in the original STR string is returned.

Limit An integer that limits the number of fragments returned. When provided, the split method splits the string on each occurrence of the specified delimiter, but stops when the restricted item has been put into the array. If the end of the string is reached before the specified limit is reached, it may still contain fewer entries than the limit. The rest of the text is not returned in the new array.

Note: If an empty string (“) is used as the delimiter, the string is not between each user-aware character (grapheme cluster), nor between each Unicode character (code point), but between each UTF-16 code unit. This destroys the proxy pair.

let names = "Harry Trump ; Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
console.log(names); // Harry Trump ; Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand
let re = /\s*(? :; |$)\s*/;
let nameList = names.split(re);
console.log(nameList); // [ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]

let myString = "Hello World. How are you doing?";
let splits = myString.split("".3);

console.log(splits); // ["Hello", "World.", "How"]
Copy the code

4. Splicing method

a)str.concat(str2, [, …strN])

The concat() method merges one or more strings with the original string concatenation to form a new string and returns it.

Return value: A new string containing the connection string provided by the argument.

Note: It is usually easier to concatenate strings with the “+” operator.

let hello = 'Hello, '
console.log(hello.concat('Kevin'.'. Have a nice day.')) // Hello, Kevin. Have a nice day.

let greetList = ['Hello'.' '.'Venkat'.'! ']
"".concat(... greetList)// "Hello Venkat!"

"".concat({})    // [object Object]
"".concat([])    / / ""
"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4.5)  45 "/ /"
Copy the code

5. Intercept substring methods based on subscripts

a)str.slice(beginIndex[, endIndex])

The slice() method extracts a portion of a string and returns a new string, leaving the original string unchanged.

Parameters:

BeginIndex extracts characters from the original string starting at the index (base 0). If the value is negative, it is treated as strLength + beginIndex, where strLength is the length of the string (for example, if beginIndex is -3 it is treated as strleng-3).

EndIndex optional. The extraction string ends at the index (base 0). If omitted, slice() extracts all the way to the end of the string. If this parameter is negative, it is treated as strLength + endIndex, where strLength is the length of the string (for example, strleng-3 if endIndex is -3).

Return value: Returns a new string extracted from the original string

Note:

A new string. Includes the string stringObject all characters from start (inclusive) to end (inclusive).

The String methods slice(), substring(), and substr() (not recommended) can all return specified portions of a String. Slice () is more flexible than Substring () because it allows negative numbers as arguments. Slice () differs from substr() in that it specifies a substring with a two-character position, whereas substr() specifies a substring with a character position and length. Also note that string.slice () is similar to array.slice ().

let str1 = 'The morning is upon us.'.// The length of str1 is 23.
    str2 = str1.slice(1.8),
    str3 = str1.slice(4, -2),
    str4 = str1.slice(12),
    str5 = str1.slice(30);
console.log(str2); // "he morn"
console.log(str3); // "morning is upon u"
console.log(str4); // "is upon us."
console.log(str5); / / ""

let str6 = 'The morning is upon us.';
str6.slice(-3);     // "us."
str6.slice(-3, -1); // "us"
str6.slice(0, -1);  // "The morning is upon us"
Copy the code

b)str.substring(indexStart[, indexEnd])

The substring() method returns a subset of a string from the start index to the end index, or from the start index to the end of the string.

Parameter: start required. A non-negative integer specifying the position in stringObject of the first character of the substring to be extracted. The stop is optional. A non-negative integer that is 1 more than the position in stringObject of the last character of the substring to be extracted. If omitted, the substring is returned up to the end of the string.

Return value: a new string containing a substring of stringObject that contains all characters from start to stop-1 and is stop minus start.

Note: The substring() method returns a string that includes characters at start, but not at stop. If the arguments start and stop are equal, the method returns an empty string (that is, a string of length 0). If start is larger than stop, the method swaps these two arguments before extracting the substring.

let anyString = "Mozilla";

/ / output Moz ""
console.log(anyString.substring(0.3));
console.log(anyString.substring(3.0));
console.log(anyString.substring(3, -3));
console.log(anyString.substring(3.NaN));
console.log(anyString.substring(-2.3));
console.log(anyString.substring(NaN.3));

/ / output "lla"
console.log(anyString.substring(4.7));
console.log(anyString.substring(7.4));

/ / output ""
console.log(anyString.substring(4.4));

/ / output Mozill ""
console.log(anyString.substring(0.6));

/ / output "Mozilla"
console.log(anyString.substring(0.7));
console.log(anyString.substring(0.10));
Copy the code

6. Intercept substring according to length

a)str.substr(start[, length])

The substr() method returns a string of characters from the specified position up to the specified number of characters.

Warning: Although String.prototype.substr(…) It is not strictly removed (as in “removed from the Web standards”), but it is considered a legacy function and should be avoided if possible. It is not part of the JavaScript core language and will probably be removed in the future. If possible, use substring() instead.

Parameters:

Start Starts to extract the character position. If it is negative, it is treated as strLength + start, where strLength is the length of the string (for example, if start is -3, it is treated as strLength + (-3)).

Length optional. The number of characters extracted.

Return value: a new string containing the length of characters starting from stringObject’s start (including the character to which start refers). If length is not specified, the string returned contains characters from start to the end of stringObject.

Note: The argument to substr() specifies the start and length of the substring, so it can be used instead of substring() and slice().

let str = "abcdefghij";

console.log("(1,2): "    + str.substr(1.2));   BC "/ /" (1, 2) :
console.log("(-3,2): "   + str.substr(-3.2));  (3, 2) : / / "hi"
console.log((3) : ""     + str.substr(-3));    // "(-3): hij"
console.log((1) : ""      + str.substr(1));     // "(1): bcdefghij"
console.log("(-20, 2): " + str.substr(-20.2)); // "(-20, 2): ab"
console.log("(20, 2)."  + str.substr(20.2));  / / "(20, 2)."
Copy the code

String created by method of string