Common JS String Methods

来源:互联网 发布:当前大数据发展趋势 编辑:程序博客网 时间:2024/04/29 22:24

MethodDescriptioncharAt()Returns the character at the specified index (position)charCodeAt()Returns the Unicode of the character at the specified indexconcat()Joins two or more strings, and returns a copy of the joined stringsfromCharCode()Converts Unicode values to charactersindexOf()Returns the position of the first found occurrence of a specified value in a stringlastIndexOf()Returns the position of the last found occurrence of a specified value in a stringlocaleCompare()Compares two strings in the current localematch()Searches a string for a match against a regular expression, and returns the matchesreplace()Searches a string for a value and returns a new string with the value replacedsearch()Searches a string for a value and returns the position of the matchslice()Extracts a part of a string and returns a new stringsplit()Splits a string into an array of substringssubstr()Extracts a part of a string from a start position through a number of characterssubstring()Extracts a part of a string between two specified positionstoLocaleLowerCase()Converts a string to lowercase letters, according to the host's localetoLocaleUpperCase()Converts a string to uppercase letters, according to the host's localetoLowerCase()Converts a string to lowercase letterstoString()Returns the value of a String objecttoUpperCase()Converts a string to uppercase letterstrim()Removes whitespace from both ends of a stringvalueOf()Returns the primitive value of a String object

IndexOf(), getting the first occurrence of a specified text in string. JavaScript counts positions from zero.

<!DOCTYPE html><html><body><p id="p1">Please locate where 'locate' occurs!.</p><button onclick="myFunction()">Try it</button><p id="demo"></p><script>function myFunction() {    var str = document.getElementById("p1").innerHTML;    var pos = str.indexOf("locate");    document.getElementById("demo").innerHTML = pos;}</script></body></html>


search(). Searching for a string in a string.

<!DOCTYPE html><html><body><p id="p1">Please locate where 'locate' occurs!.</p><button onclick="myFunction()">Try it</button><p id="demo"></p><script>function myFunction() {    var str = document.getElementById("p1").innerHTML;    var pos = str.search("locate");    document.getElementById("demo").innerHTML = pos;}</script></body></html>


Slice() Method:

slices out a portion of a string from position 7 to position 13

<script>var str = "Apple, Banana, Kiwi";document.getElementById("demo").innerHTML = str.slice(7,13);</script>
If a parameter is negative, the position is counted from the end of the string.

<span style="font-size:12px;"><script>var str = "Apple, Banana, Kiwi";document.getElementById("demo").innerHTML = str.slice(-12,-6);</script></span>
If the second parameter is omitted, the method will slice out all the rest string. Or counting from the end.

var res = str.slice(7);var res = str.slice(-12);

substring() is similar to slice(), but can not accept negative values.
substr() is similar to slice(), the difference is that the second parameter specify the length of the extracted text.

replace() method replaces a specified value with another value in a string.

var n = str.replace("orginal", "replaced");
toUpperCase() convert string to upper case.

var str = "Hello world!"var N = str.toUpperCase(); //returns HELLO WORLD!
toLowerCase() is similarly used.

concat() joins two or more strings together.

var text1 = "Hello";var text2 = "World";var text3 = text1.concat(" ", text2); // returns Hello World


charAt() returns the character in a position.

var str = "Hello world!";str.charAt(1);  //returns e


charCodeAt() return the unicode of the character.

var str = "HELLO WORLD!";str.charCodeAt(0);      //returns 72


split() convert a string into an array

<!DOCTYPE html><html><body><p id="demo"></p><script>var str = "Hello";var arr = str.split("");var text = "";var i;for (i = 0; i < arr.length; i++) {    text += arr[i] + "<br>"}document.getElementById("demo").innerHTML = text;</script></body></html>



Reference:http://www.w3schools.com/js/js_strings.asp

0 0