String methods commonly used in JavaScript

1. charAt(x)

charAt(x)Returns string xcharacter at that position, from the index 0beginning.

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

2. charCodeAt(x)

`charCodeAt(x)`返回字符串中`x`位置处字符的`unicode`值。    
// charAt(position)    
var message="jquery4u"    
//alert "113"    
alert(message.charAt(1)

3. concat(v1,v2..)

concat() A method for connecting two or more strings, this method does not change the existing string, returns the new string of splicing.

//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)Converting a set of Unicodevalues into a character.

//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])

indexOfThe method of search and (if found) returns the index of the string to search for the character or substring. If not found, it returns -1. StartIt is an optional parameter that specifies the start position of the search string, the default value 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])

lastIndexOf()Method returns the index of the specified text string of the last occurrence of, if not found, it returns -1. " " StartIt is an optional parameter that specifies the start position of the search string, the default value string.length-1.

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

7. match(regexp)

According to the regular expression matches the search string. If a match is not found, a message is returned or array null.

//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)

replace() A method for replacing with other characters in the character string number, or alternatively a substring matching the positive 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)

search()A method for substring specified search character string, or retrieve regular expression matching substring, if found, return to the regexpstarting position of the substring match, 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])

slice()The method may extract a portion of the string, it returns a new string. Including the string from startthe beginning (including the start) to endend (not including endall characters) up.

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

11. split(delimiter, [limit])

split()A method for dividing a character string into a string array, return the string array to return a string array that does not include delimiterits own. Optional " limit" is an integer that allows you to specify the maximum number of elements in the array to return.

12. substr(start, [length])

substr()The method can be extracted from the string startspecified number of characters starting index. Returns a new string, comprising from startstarting (including the start character referred to) at lengthcharacters. If not specified length, then the returned string contains from startthe end of the string of characters.

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

13. substring(from, [to])

substring()A method for extracting the character string intermediary between two specified index, comprising substring is returned from startthe character, but does not include stopthe character, and tooptionally, if the argument is omitted, then the substring will always return the end of the string.

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

14. toLowerCase()

toLowerCase() A method for converting a string to lowercase.

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

15. toUpperCase()

toUpperCase() The method used to convert a string to upper case.

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

16. includes()

includes() A method for checking whether a string contains the specified string or character.

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

17. endsWith()

endsWith()Function checks whether the specified string to the end of the 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 comprising a specified number of copies of the string are connected together.

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

19. valueOf()

valueOf()The method returns an Stringobject original value (primitive value), this value is identical String.prototype.toString().

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

20. trim()

trim()The method removes the whitespace characters from both ends of a string. Blank characters in this context is that all whitespace (space, tab, no-break space, etc.), and all line terminator character (e.g., LF, CR).

//trim()    
var str = "     Hello Edureka!     ";
alert(str.trim());

Guess you like

Origin www.cnblogs.com/liudemeng/p/11598707.html