JS common string 20 before use and methods

To read more quality articles please Mengchuo GitHub blog , nearly 100 articles a year of high-quality articles waiting for you!

In order to repay the reader, the "big move in the world", held from time to time (one to three times a month), cash raffle, security at the end 200, plus user appreciation, I hope you can become a big move in the world of small carp, come try it

This paper describes some of the most commonly used JSstring functions listed below:

  • 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 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)Returns a string xat a character position unicodevalue.

//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());
复制代码

After the code is deployed may exist BUG can not know in real time, and afterwards in order to solve these BUG, we spent a lot of time debugging log, here for everyone to recommend a way BUG easy to use monitoring tools Fundebug .

communicate with

Dry series are summarized below, feel good point of a Star, please add the group to learn from each other.

github.com/qq449245884…

I am a little wisdom, public numbers "big move in the world," the author, to keep learning enthusiasts of front-end technology. I would often share their learned the dry look , the way in advanced, encourage each other!

No public attention, background replies welfare , you can see the benefits, you know.

Guess you like

Origin juejin.im/post/5d6866936fb9a06ada54c642