Method | Description |
---|---|
charAt() | Returns the character at the specified index (position) |
charCodeAt() | Returns the Unicode of the character at the specified index |
concat() | Joins two or more strings, and returns a copy of the joined strings |
fromCharCode() | Converts Unicode values to characters |
indexOf() | Returns the position of the first found occurrence of a specified value in a string |
lastIndexOf() | Returns the position of the last found occurrence of a specified value in a string |
localeCompare() | Compares two strings in the current locale |
match() | Searches a string for a match against a regular expression, and returns the matches |
replace() | Searches a string for a value and returns a new string with the value replaced |
search() | Searches a string for a value and returns the position of the match |
slice() | Extracts a part of a string and returns a new string |
split() | Splits a string into an array of substrings |
substr() | Extracts a part of a string from a start position through a number of characters |
substring() | Extracts a part of a string between two specified positions |
toLocaleLowerCase() | Converts a string to lowercase letters, according to the host's locale |
toLocaleUpperCase() | Converts a string to uppercase letters, according to the host's locale |
toLowerCase() | Converts a string to lowercase letters |
toString() | Returns the value of a String object |
toUpperCase() | Converts a string to uppercase letters |
trim() | Removes whitespace from both ends of a string |
valueOf() | 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 οnclick="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>
<!DOCTYPE html>
<html>
<body>
<p id="p1">Please locate where 'locate' occurs!.</p>
<button οnclick="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>
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
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