This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Hello everyone, I am the prosperity of the other shore 🌸, a front-end 👨🏻💻 that firmly believes that hard work can change destiny. If I have the honor to write an article that can get your favor, I am very lucky ~

This series of articles was first published in nuggets

Writing in the front

In this article, we will learn about strings in JavaScript. What can we learn from this article? The diagram below:

The String object can be used to define strings, and the basic data type String can also use the methods provided by String. Strings provide a series of methods that can perform a variety of functions, which can be classified as follows:

  • Case conversion

  • Gets a string at the specified location

  • Retrieve string

  • Intercept string

  • Split string

  • Connection string

  • Regular match

Case conversion

There are two ways to convert a String to case:

  • ToUpperCase () : converts a string toUpperCase and returns the converted result

  • ToLowerCase () : converts a string toLowerCase and returns the converted result

The example code is as follows:

var msg = 'Hello World'

var lowerMsg = msg.toLowerCase()
var upperMsg = msg.toUpperCase()

console.log(msg);// Hello World
console.log(lowerMsg);// hello world
console.log(upperMsg);// HELLO WORLD
Copy the code

It is worth noting that Chinese is not case sensitive. Therefore, the above case conversion method should be used for characters other than Chinese that require upper and lower case forms.

Gets a string at the specified location

The String object provides three methods to get the character at the corresponding position in a String based on the index value. These methods are often used to take out the specified characters for logical judgment and other application scenarios. The specific methods are shown in the following table:

methods describe
charAt() Returns a character for a specific position
charCodeAt() Returns the Unicode value of the character representing the given index
codePointAt() Returns a nonnegative integer of the value at the given position encoded in UTF-16

The following code example shows how to use this method:

var str = 'Hello World! '

var charAt = str.charAt(2)
var charCodeAt = str.charCodeAt(2)
var codePointAt = str.codePointAt(2)

console.log(charAt, charCodeAt, codePointAt)  // l 108 108
Copy the code

From the above code we know that the Hello World string with index 2 is an L character with a Unicode value of 108.

Retrieve string

In addition to providing a way to look up characters in a String by their index value, the String object provides two other ways to look up the corresponding index value by character or substring. If the specified character or substring does not exist in a string, the search results in -1.

IndexOf () method

The indexOf method provided by the String object is used to look for the first index in the String that matches the given value, and returns -1 if none is found. This method takes a string of arguments, the first representing the string to be found; The second argument is the starting position, which is optional and defaults to 0.

The example code is as follows:

var email = '[email protected]'

// indexOf() takes two arguments, the first indicating the string to retrieve and the second indicating the starting position, which defaults to 0

// Check whether the email string contains the qq string
console.log(email.indexOf('qq')) / / 8
// Check whether the email string contains the string o
console.log(email.indexOf('o')) / / 4
// Look for o in the email string, starting at index 8
console.log(email.indexOf('o'.8)) / / 12
Copy the code

The lastIndexOf () method

The lastIndexOf() method provided by strings is similar to indexOf except that it looks for the last matching letter and returns the index.

The example code is as follows:

var email = '[email protected]'

// lastIndexOf() finds the last matched string index
console.log(email.lastIndexOf('o')) / / 12
Copy the code

Intercept string

The String object provides two methods for intercepting a String. These methods return the substring that is intercepted according to the rule, and do not change the content of the original String.

Slice () method

The slice() method truncates the contents of a string based on where the truncation started and where the truncation ended, and returns the truncated substring as a new string. This method takes two arguments, a starting index and an ending index.

Note that this method allows you to pass a negative index. Passing a negative number is treated as the length of the string plus a negative number.

The example code is as follows:

var str = 'JavaScript'

var result1 = str.slice(0.4)
-10 + 10 = 0; -6 + 10 = 4; -6 + 10 = 4; -6 + 10 = 4
var result2 = str.slice(-10, -6)

console.log(result1, result2) // Java Java
Copy the code

The substring () method

The substring() method has the same functions and parameters as the slice() method, except that the substring() method uses a negative number as a 0.

The example code is as follows:

var str = 'JavaScript'

// Passing negative numbers is treated as 0 and the final result is null
var result1 = str.substring(0, -1)
var result2 = str.substring(0.4)
console.log(result1, result2) // Java

Copy the code

It’s worth noting that the substring() method’s two arguments have the following properties:

  • If the starting index is equal to the ending index, return an empty string.

  • If the trailing index is omitted, the extract is carried to the end of the string.

  • If the human argument is less than 0 or NaN, it is used as a 0.

  • If any argument is greater than the length of the string, the final value is the length of the string.

  • If the starting index is greater than the ending index, the starting index is swapped with the ending index.

Split stringsplit()methods

The split() method is used to split a string into an array according to the specified separator. The method takes two arguments. The first argument can be either a string or a regular expression representing the delimiter that separates the string. The other parameter means to limit the number of fragments returned, which is optional.

The example code is as follows:

var str = 'Lucy,Mary,Lily,Emma'

// Use the string as the split symbol
var result1 = str.split(', ')
// Qualify to return two
var result2 = str.split(', '.2)

console.log(result1, result2) // [ 'Lucy', 'Mary', 'Lily', 'Emma' ] [ 'Lucy', 'Mary' ]
Copy the code

Connection string

The concat() method provided by String objects is used to concatenate one or more strings to form a new String and return it. This method takes N arguments and returns the concatenation result of the string. The example code is as follows:

var str = 'JavaScript '
var result = str.concat('Python '.'Go')
console.log(result) // JavaScript Python Go
Copy the code

It should be noted that using the string concatenator, the + sign, performs better than concat()

Regular match

Regular expressions are primarily used in conjunction with strings. Strings in JavaScript provide three methods for using regular expressions.

We’ll talk about regular expressions in the RegExp Objects article

Search () method

The search() method searches a string to see if there is a substring that corresponds to the specified regular expression. Returns the index of the first match if it exists, or -1 if it does not.

The example code is as follows:

var data = 'If the other shore bustling fall, who accompany me to see the sunset fleeting years'

console.log(data.search(/ busy /)) / / 3
Copy the code

Match () method

The difference between the match() method and the search() method is that it returns the result of a match. The example code is as follows:

var data = 'If the other shore bustling fall, who accompany me to see the sunset fleeting years'

// match() returns a string that matches the regular expression
console.log(data.match(/ busy /)) // [' 上 海 ', index: 3, input: '上 海 ', index: 3, input:' 上 海 ', index: 3, input: '上 海 ', group: undefined]
Copy the code

This method returns null if the match fails. An array is returned after a successful match. The first item of the array represents the matched literal content, followed by the index and the entire content

The replace () method

The replace() method is used to retrieve the specified regular expression in a string, replace the one that matches it, and return the replaced string. This method takes two arguments, the first being the regular expression and the second being what you want to replace.

The example code is as follows:

var data = 'If the other shore bustling fall, who accompany me to see the sunset fleeting years'
The replace() method is used to replace the content that matches the regular expression
console.log(data.replace(/ busy /.'busy')) // If the other shore bustling fall, who accompany me to see the sunset time
Copy the code

conclusion

In addition to strings, there are Boolean objects and Number objects. The same variables of type Boolean and Number can also use the properties and methods they provide, but these two don’t provide many properties and methods that are commonly used, so I won’t cover them here.

Preview: We’ll look at Array objects in JavaScript in the next article

Excellent articles

11- Objects in JavaScript

Scope in JavaScript

09-JavaScript functions (all basic, see if you can do it)

The basics of arrays in JavaScript may be something you don’t know

07- Learn to loop in JavaScript