Common JS String Methods

MethodDescription
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>


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

<!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>


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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值