Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 20 string methods commonly used in JavaScript

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What are the 20 string methods commonly used in JavaScript? in order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

This article introduces 20 commonly used JavaScript string methods that are worth collecting. There is a certain reference value, friends in need can refer to, hope to help you.

1. CharAt (x)

CharAt (x) returns the character of the x position in the string, with the subscript starting at 0.

/ / charAt (x) var myString = 'jQuery FTWs for recent years; console.log (myString.charAt (7)); / / output: F

2. CharCodeAt (x)

`unicode` returns the `unicode` value of the character at the position `x` in the string `charCodeAt (x) `. / / charAt (position) var message= "jquery4u" / / alert "113" alert (message.charAt (1)

3. Concat (v1Jing v2.)

The concat () method is used to concatenate two or more strings. This method does not change the existing string, but returns the new concatenated string.

/ / concat (v1, v2jie..) Var message= "Sam" var final=message.concat ("is a", "hopeless romantic.") / / alerts "Sam is a hopeless romantic." Alert (final)

4. FromCharcode (C1 and c2)

FromCharcode (C1 ~ c2) converts a set of Unicode values to characters.

/ / fromCharCode (C1, C2...) Console.log (String.fromCharCode (99120121122)) / / output: abcxyz console.log (abcxyz console.log (72recollection, 69pr, 76pr, 76pr 79)) / / output: HELLO

5. IndexOf (substr, [start])

The indexOf method searches and, if found, returns the index of the character or substring found in the string. If not found,-1 is returned. Start is an optional parameter that specifies the location in the string where the search starts. The default value is 0.

/ / indexOf (char/substring) var sentence= "Hi, my name is Sam!" If (sentence.indexOf ("Sam")! =-1) alert ("Sam is in there!")

6. LastIndexOf (substr, [start])

The lastIndexOf () method returns the index of the last occurrence of the specified text in the string, or-1 if not found. "Start" is an optional parameter that specifies the location in the string where the search starts. The default value is string.length-1.

/ / lastIndexOf (substr, [start]) var myString = 'javascript rox'; console.log (myString.lastIndexOf (' r')); / / output: 11

7. Match (regexp)

Searches for a match in a string based on a regular expression. If no match is found, an array of information or null is returned.

/ / match (regexp) / / select integers only var intRegex = / [0-9-() +] + $/; var myNumber = '999; var myInt = myNumber.match (intRegex); console.log (isInt); / / output: 999 var myString =' 999 JS Coders'; var myInt = myString.match (intRegex); console.log (isInt); / / output: null

8. Replace (regexp/substr, replacetext)

The replace () method is used to replace some characters in a string with other characters, or to replace a substring that matches a regular expression.

/ replace (substr, replacetext) var myString = '999 JavaScript Coders'; console.log (myString.replace (/ JavaScript/i, "jQuery")); / / output: 999 jQuery Coders / / replace (regexp, replacetext) var myString =' 999 JavaScript Coders'; console.log (myString.replace (new RegExp ("999", "gi"), "The")); / / output: The JavaScript Coders

9. Search (regexp)

The search () method is used to retrieve a specified substring in a string, or to retrieve a substring that matches a regular expression, and if found, returns the starting position of the substring that matches regexp, otherwise-1.

/ / search (regexp) var intRegex = / [0-9-() +] + $/; var myNumber = '999; var isInt = myNumber.search (intRegex); console.log (isInt); / / output: 0

10. Slice (start, [end])

The slice () method extracts a portion of a string and returns a new string. Includes all characters whose string begins with start (including start) and ends with end (excluding end).

/ / slice (start, end) var text= "excellent" text.slice (0ju 4) / / returns "exce" text.slice (2pm 4) / / returns "ce"

11. Split (delimiter, [limit])

The split () method is used to split a string into an array of strings, and returns an array of strings that do not include the delimiter itself. The optional "limit" is an integer that allows you to specify the number of elements of the maximum array to return.

12. Substr (start, [length])

The substr () method extracts a specified number of characters from a string starting with the start subscript. Returns a new string containing length characters starting at start (including the characters referred to by start). If no length is specified, the returned string contains the characters from start to the end of the string.

/ / substring (from, to) var text= "excellent" text.substring (0ju 4) / / returns "exce" text.substring (2pm 4) / / returns "ce"

13. Substring (from, [to])

The substring () method is used to extract the character of a string mediating between two specified subscripts. The returned substring includes the character at start, but not the character at stop. To is optional. If this parameter is omitted, the returned substring will go all the way to the end of the string.

/ / substring (from, [to]) var myString = 'javascript rox'; myString = myString.substring (0Magne10); console.log (myString) / / output: javascript

14. ToLowerCase ()

The toLowerCase () method is used to convert strings to lowercase.

/ / toLowerCase () var myString = 'JAVASCRIPT ROX'; myString = myString.toLowerCase (); console.log (myString) / / output: javascript rox

15. ToUpperCase ()

The toUpperCase () method is used to convert strings to uppercase.

/ / toUpperCase () var myString = 'javascript rox'; myString = myString.toUpperCase (); console.log (myString) / / output: JAVASCRIPT ROX

16. Includes ()

The includes () method is used to check whether the string contains the specified string or character.

/ / includes () var mystring = "Hello, welcome to edureka"; var n = mystring.includes ("edureka"); / / output: True

17. EndsWith ()

The endsWith () function checks whether the string ends with the specified string or character.

/ / endsWith () var mystr = "List of javascript functions"; var n = mystr.endsWith ("functions"); / / output: True

18. Repeat ()

Repeat () constructs and returns a new string containing copies of the specified number of strings concatenated together.

/ / repeat () var string = "Welcome to Edureka"; string.repeat (2); / / output: Welcome to Edureka Welcome to Edureka

19. ValueOf ()

The valueOf () method returns the original value (primitive value) of a String object, which is equivalent to String.prototype.toString ().

/ / valueOf () var mystr = "Hello World!"; var res = mystr.valueOf (); / / output: Hello World!

20. Trim ()

The trim () method removes white space characters from both ends of a string. The white space characters in this context are all white space characters (space, tab, no-break space, etc.) and all line Terminator characters (such as LF,CR)

/ / trim () var str = "Hello Edureka!"; alert (str.trim ()); the answers to the questions about the 20 string methods commonly used in JavaScript are shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report