Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

In the previous article, I learned the concept of string objects in JavaScript and how to create them. Today, I reviewed the common properties of strings and common methods.

Common properties of string objects

The String type is one of the basic data types in JavaScript. It also has fewer attributes, such as length.

There are also two other properties:

  1. Constructor: represents the functional model of character objects
  2. prototype: adds attributes to string objects

Object attributes are in the following format:

  • Object name. Attribute name // Indicates the value of the object attribute
  • Attribute name = attribute value // Indicates the assignment operation for the attribute

Declare a string object: myString, and print the number of characters in it

const myString =
  '[Nuggets] Feel tired to rest, in fact, not so good, do not have to carry a person, you want to like ordinary people have the most simple happiness. '

document.write(mySring.length) // Outputs the number of characters in the string object
Copy the code

Common functions for string objects

The string object is a common object and a built-in object in JavaScript. When operating on strings: find/replace characters are often used..

To facilitate string manipulation, JavaScript has a number of built-in methods that we can use to accomplish our requirements.

Common functions for string objects:

1. CharAt (position): string object a character at the specified position

myString.charAt(0) / / '['
myString.charAt(1) / / 'dig'
myString.charAt(2) / / 'gold'
/ /... And so on
Copy the code

2. CharCodeAt (position):

Outputs the Unicode value of the character at the position specified by the string object

myString.charCodeAt(1) // 'dig' --> 25496
myString.charCodeAt(2) // 'gold' --> 37329
Copy the code

3. IndexOf (String to find, [starting position]):

Finds the location of the substring from front to back, starting at the specified position in the string.

myString.indexOf('the nuggets') // 1 starts at index 1
Copy the code

4. IndexOf (String to find):

Finds the location of the substring backward, starting at the specified position in the string. And (3) instead

5. Split ([separator]): ❤ Commonly used ❤

An array of delimited strings with a given delimiter is usually used to process the format of Url date coins

myString.split(' ') // Delimited with "", i.e. each character is an element of the array
/ / [' (', 'dig', 'gold', ') ', 'feeling', 'night', 'tired', 'is',' Sue ', 'interest', 'Sue', 'interest', ', ', 'the', 'real', 'no', 'with', 'the', 'it', 'best', 'show', ', ', 'not' and 'will', 'a', 'a', 'people' and 'shoulder', ', ', 'you', 'to', 'like', 'who', ', ', 'vulgar', 'child', 'a', 'sample', 'a', 'the', 'Jane', 'single', 'the', 'fast', 'music', '. ']
Copy the code

6. Replace (string to be replaced, new string):

Finds the specified string in a string and replaces it with a new string

myString.replace('the nuggets'.'Rare Earth Mining Technology Community') / / replace
Copy the code

7. SubStr (start position, length):

Intercepts a character of the given length, starting at the specified position of the string object, and returns the intercepted character

8. SubString (start position, end position):

Intercepts the string to the given end position, starting at the position specified by the string, and returns the intercepted character

9. .toLowerCase(): Changes the uppercase letter in the string object to lowercase

10. .toUpperCase(): Change lowercase letters to uppercase letters as opposed to above.