JS Top 20 Common String Methods and Usage

Keywords: Javascript JQuery github

To read more good articles, please Punch GitHub Blog Hundreds of excellent articles a year are waiting for you!

In order to give back to the reader, Daqian World is held on an irregular basis (one to three times a month), a cash lottery, a bottom 200, plus user appreciation. Hope you can become a little brocade in Daqian World. Try it now

This article mainly introduces some of the most commonly used JS string functions, which are listed as follows:

  • charAt(x)
  • charCodeAt(x)
  • concat(v1,v2..)
  • fromCharcode(c1,c2)
  • indexOf(substr, [start])
  • lastIndexOf(substr, [start])
  • match(regexp)
  • replace(regexp/substr, replacetext)
  • search(regexp)
  • slice(start, [end])
  • split(delimiter, [limit])
  • substr(start, [length])
  • substring(from, [to])
  • toLowerCase()
  • toUpperCase()
  • includes()
  • endsWith()
  • repeat()
  • valueOf()
  • trim()

1. charAt(x)

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

//charAt(x)
var myString = 'jQuery FTW!!!';
console.log(myString.charAt(7));
//output: F

2.charCodeAt(x)

charCodeAt(x) returns the unicode value of the character at the x position in the string.

//charAt(position)
var message="jquery4u"
//alert "113"
alert(message.charAt(1)

3.concat(v1,v2..)

The concat() method is used to join two or more strings. It does not change the existing string, but returns the stitched new string.

//concat(v1, v2,..)
var message="Sam"
var final=message.concat(" is a"," hopeless romantic.")
//alerts "Sam is a hopeless romantic."
alert(final)

4.fromCharcode(c1,c2)

fromCharcode(c1,c2) converts a set of Unicode values to characters.

//fromCharCode(c1, c2,...)
console.log(String.fromCharCode(97,98,99,120,121,122))
//output: abcxyz
console.log(String.fromCharCode(72,69,76,76,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 it is not found, it returns -1.Start is an optional parameter that specifies where in the string to start the search, with a default value of 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 it is not found."Start" is an optional parameter that specifies where in the string to start the search, with a default value of string.length-1.

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

7.match(regexp)

Searches for matches in strings based on regular expressions.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 others, or 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 retrieves the substring specified in the string, or the substring matching the regular expression. If found, it returns the starting position of the substring matching the regexp, or -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 part of a string and returns a new string.Includes all characters of a string from start (including start) to end (excluding end).

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

11.split(delimiter, [limit])

The split() method splits a string into an array of strings and returns an array of strings that returns an array of strings that does not include 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 the start, including the character referred to by the start.If no length is specified, the returned string contains characters from start to the end of the string.

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

13.substring(from, [to])

The substring() method extracts the character between two specified subscripts in a string and returns a substring that includes the character at the start, but not the character at the stop. To is optional. If this parameter is omitted, the returned substring will continue to the end of the string.

//substring(from, [to])
var myString = 'javascript rox';
myString = myString.substring(0,10);
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 include() method checks whether a string contains a 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 a 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 a specified number of strings that are joined 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.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());

BUGs that may exist after code deployment are not known in real time. In order to solve these BUGs afterwards, a lot of time has been spent debugging the log. By the way, a useful BUG monitoring tool is recommended. Fundebug.

Communication

The dry goods series articles are summarized below, feel good to order Star, welcome to join the group to learn from each other.

https://github.com/qq44924588...

I am Xiao Zhi, the author of the public number "Move the World", a fan of keeping learning about front-end technology.I often share what I've learned and what I've seen. On the way to the next level, I'll reluctantly!

Focus on the public number and reply to the benefits in the background. You can see the benefits, you know.

Posted by kabucek on Thu, 29 Aug 2019 17:05:14 -0700