String processing in JavaScript

Tram Ho

String processing in JavaScript is a basic skill that any developer needs to know, similar to array, String is constantly improving and updating by developers. Up to now, there must also be 9,000 8 thousand different methods of processing chains to contribute to making developers more brain pain when using. Let’s learn about popular methods with me through this article!

1. String.prototype.charAt ()

  • Returns the character in the position specified by the index. For example the first position is 0 followed by 1, 2 …
  • If index is out of range, an empty string is returned

Syntax

2. String.prototype.charCodeAt ()

  • Returns an integer between 0 and 65535 accordance with the UTF-16 standard
  • If index is out of range, NaN returned

Syntax

3. String.prototype.concat ()

Usually to concatenate strings we often use the + sign, however JS provides us with this method with similar functionality

  • This method returns a new string after concatenation

Syntax

4. String.prototype.includes ()

  • Used to check whether a string can be found in another string
  • Returns true if found, or false otherwise

Syntax

5. String.prototype.indexOf ()

  • Used to find the position of a string in another string
  • Returns the first position found (starting from the position specified to be searched) or -1 if not found

Syntax

6. String.prototype.lastIndexOf ()

In contrast to indexOf() , it starts looking at the end of the string

7. String.prototype.match ()

Regex in JS is something very cool and this method is often used when working with Regex

  • Returns the result of a string match with a regular expression .
  • Returns an array containing matches or null if not found

Syntax

8. String.prototype.replace ()

  • This method is used a lot when you want to replace or delete characters in string
  • The string to be replaced can be a simple string or a regular expression

Syntax

9. String.prototype.search ()

  • Used to find the position of a string in another string
  • Returns the found position or -1 otherwise
  • The search pattern can be a string or a regular expression

Syntax

10. String.prototype.slice ()

  • Used to extract part of the string and return it as a new string without changing the original string
  • This method is quite similar to slice() in Array that I introduced to you in the previous article

Syntax

Conclusion

The article is quite long, then I temporarily stop here and will continue to present some more methods in the next article.

If you find a good article, give me +1 upvote. If you like me, please press the follow button for more interesting things. Good luck !

Share the news now

Source : Viblo