Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the string API commonly used in JavaScript

Shulou Source: shulou.com Published: 2022-06-03 08:14:15 09月19日 Update

In this issue, the editor will bring you about the string API commonly used in JavaScript. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The string API commonly used in JavaScript

Length:

Gets the length of the string

Var str= "abcde"

Console.log (str.length); / / 5

CharAt ():

Returns the character of the specified position

Var str= "abcde"

Console.log (str.charAt (0)); / / a

Console.log (str.charAt (3)) / / d

CharCodeAt ():

Returns the Unicode encoding of the characters at the specified position

Var str= "abcde"

Console.log (str.charCodeAt (0)); / / 97

Concat ():

Concatenate two or more strings

Var str= "abcde"

Console.log (str.concat ("ffff")); / / abcdeffff

IndexOf ():

Find the subscript corresponding to a string, and return-1 if it cannot be found

Var str= "abcde"

Console.log (str.indexOf ('c')); / / 2

Console.log (str.indexOf ('g')); / /-1

LastIndexOf ():

Check the last subscript of a string, and return-1 if it cannot be found

Var str= "abcde"

Console.log (str.lastIndexOf ('b')); / / 1

Console.log (str.lastIndexOf ('r')); / /-1

Slice (start,end):

Intercept the string, the subscript at the beginning of start and the subscript at the end of end, without the end itself; if end is null, the subscript is intercepted to the end

Var str= "abcde"

Console.log (str.slice (0Pol 3)); / / abc

Substr (start,count):

Intercept string, subscript starting with start, length intercepted by count. If count is null, the last truncation is obtained. If start is negative, it is reciprocal.

Var str= "abcde"

Console.log (str.substr (0jue 2)); / / ab

Console.log (str.substr (- 2)); / / de

Substring (start,end):

Intercept the string, the subscript of the beginning of start, the subscript of the end of end, does not contain the end itself. If the end is empty, it is intercepted to the end. If the subscript is negative, it is automatically converted to 0.

Var str= "abcde"

Console.log (str.substring (0Pol 3)); / / abc

Console.log (str.substring (- 3)); / / abcde

Split ():

Divide the string into an array of strings which is a good http://m.zykdtj.com/ for Zhengzhou expelling birthmark hospital

Var str= "abcde"

Console.log (str.split (')); / / ["a", "b", "c", "d", "e"]

Console.log (str.split (',')) / / ["abcde"]

Trim ():

Delete all spaces in the prefix and suffix of the string and save the result in a new string to return

Var str= "abcde"

Console.log (str.trim ()); / / abcde

ToLowerCase (), toUpperCase ():

Convert a string to lowercase, uppercase, and save it in a new string to return

Var str= "abCDe"

Console.log (str.toLowerCase ()); / / abcde

Console.log (str.toUpperCase ()); / / ABCDE

The difference between join () and split ()

Join splices an array into a string and returns it, with the parameters received by join as the separator between elements (default is ",")

Split takes a string as the split point with the received parameter

Var arr= ["a", "b", "c", "d", "e", "f"]

Var str= "a.b.c.d.e.f"

Console.log (arr.join ('.')); / / a.b.c.d.e.f

Console.log (str.split ('.)); / / ["a", "b", "c", "d", "e"

This is what the string API commonly used in JavaScript is like. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Characters strings subscript commonly used location content parameters array more length analysis professional two small and medium between elements rich content hospital suffix uppercase Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Microsoft OPPO Reno Xiaomi