Home  >  Article  >  Web Front-end  >  Summary of JavaScript string methods

Summary of JavaScript string methods

高洛峰
高洛峰Original
2017-01-14 10:32:281087browse

1. stringObject.charAt()

Function: Return the subscript of the string

var str="这是一串字符串";
console.log(str.charAt(0))//这

2. stringObject.charCodeAt()

Function: The method can return the Unicode encoding of the character at the specified position

var str="这是一串字符串";
console.log(str.charCodeAt(0))
//这=>36825

3.String.fromCharCode()

Function: Pass Unicode encoding returns the corresponding characters

console.log(String.fromCharCode(36825,26159))//这是

Example question: Find whether a string is a number

<body>
<input type="text" />
<input type="button" value="检测" />
<script>
var aInp = document.getElementsByTagName(&#39;input&#39;);
 
aInp[1].onclick = function () {
   
  var val = aInp[0].value;
   
  if ( detectNum(val) ) {
    alert( &#39;恭喜,&#39;+ val +&#39;全是数字&#39; );
  } else {
    alert(&#39;输入有误&#39;);
  }
};
function detectNum ( str ) {
  var n = 0;
  for ( var i=0; i<str.length; i++ ) {
    n = str.charCodeAt(i);
    if ( n<48 || n>57 )return false;
  }
  return true;
}
</script>
</body>

4. stringObject.indexOf()

Function: This method can return the position where a specified string value first appears in the string.
Parameters: str.indexOf (search value, start searching for subscript), if the string value to be retrieved does not appear, this method returns -1.

Example: Return the subscript where the corresponding character appears

<script>
var str = &#39;xsxsxscdecdcdxsxsxs&#39;;
var num = 0;
var s = &#39;xs&#39;;
var arr = [];
for (; str.indexOf(s, num) != -1;) {
  num = str.indexOf(s, num) + s.length
  arr.push(num)
}
console.log(arr)
</script>

5. stringObject.lastIndexOf()

Function: from back to front Find the position where a specified string value first appears in the string

6. stringObject.substring()

Function: The method is used to extract the string between two characters between specified subscripts.

7. stringObject.toUpperCase()

Function: Convert letters to uppercase

8. stringObject.toLowerCase()

Function: Convert letters to lowercase

9.stringObject.split()

Function: The method is used to split a string into a string array
Parameters: (What Character interception, retaining the number of the array)

Three usages

var str="121314";
 
str.split("") //[1,2,1,3,1,4];
 
str.split("1")//[ ,2,3,4];
 
str.split("",2)//[1,2]
10.arrObject.join()

Function: The method is used to put all the elements in the array into a string. Elements are separated by the specified delimiter

Two usages

var arr = [1,2,3];
arr.join("")//123
arr.join("-")//1-2-3

Example: Highlight the searched keyword

<input type="text" id="oin" />
<button>按钮</button>
var oin = document.getElementById("oin");
var obtn = document.getElementsByTagName(&#39;button&#39;)[0];
var str = "arguments对象的长度是由实参个数而不是形参个数决定的。
形参是函数内部重新开辟内存空间存储的变量,但是其与arguments对象
内存空间并不重叠。对于arguments和值都存在的情况下,两者值是同步的
,但是针对其中一个无值的情况下,对于此无值的情形值不会得以同步。
如下代码可以得以验证。";
var h = "";
obtn.onclick = function() {
  if (oin.value == "") {
    alert("输入为空");
    return false
  }
  var s = oin.value;
  if (str.indexOf(s) == -1) {
    alert("没有这个数字");
    return false
  }
  var m = &#39;<span style="background-color:red">&#39; + s + &#39;</span>&#39;;
  str = str.split(s);
  h = str.join(m)
  document.body.innerHTML=h
}

For more JavaScript string method summary related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn