Home>

String operations are very frequent in js,It is also very important.I can remember very clearly after reading the book,But it ’s not used for a while,They will forget about it,Bad memory is a blunder.. . Today, I will organize some common operations on strings.One deepens the impression,Both are convenient for future review.

Stringobject.charat ()

Purpose:Returns the index of a string

var str="This is a string";
console.log (str.charat (0)) //this

Stringobject.charcodeat ()

Purpose:The method returns the unicode encoding of the character at the specified position

var str="This is a string";
console.log (str.charcodeat (0))
//this =>36825

3.string.fromcharcode ()

Role:Returns the corresponding character through unicode encoding

console.log (string.fromcharcode (36825,26159)) //This is

Example:Finding whether a string is a number

<body>
<input type="text" />
<input type="button" value="detection" />
<script>
var ainp=document.getelementsbytagname ("input");
ainp [1] .onclick=function () {
  var val=ainp [0] .value;
  if (detectnum (val)) {
    alert ("Congratulations," + val + "are all numbers");
  } else {
    alert ("Incorrect input");
  }
};
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 ()

Purpose:The method returns the first occurrence of a specified string value in the string.

Parameters:str.indexof (search value, start searching for subscripts), if the string value to be retrieved does not appear,The method returns -1.

Example:return to find the subscript where the corresponding character appears

 <script>
  var str="xsxsxscdecdcdxsxsxs";
  var num=0;
  var s="xs";
  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 ()

Role:Find the first occurrence of a specified string value in a string from back to front

6. stringobject.substring ()

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

7. stringobject.touppercase ()

Role:convert letters to uppercase

8. stringobject.tolowercase ()

Role:Lowercase letters

9.stringobject.split ()

Role:The method is used to split a string into an array of strings

Parameters:(intercepted by what character,(Keep the array to the few digits)

Three uses

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 ()

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

Two uses

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

Example:Highlight the keyword you are looking for

 <input type="text" />
  <button>button</button>
  var oin=document.getelementbyid ("oin");
  var obtn=document.getelementsbytagname ("button") [0];
  var str="The length of the arguments is determined by the number of arguments, not the number of formal parameters.
  The parameter is a variable that re-opens the memory space inside the function.
But it is related to arguments object
  The memory space does not overlap.
For both arguments and values,Both values ​​are synchronized
  , But for one of which has no value,Values ​​will not be synchronized for this valueless case.
  The following code can be verified.
";
  var h="";
  obtn.onclick=function () {
    if (oin.value == "") {
      alert ("The input is empty");
      return false
    }
    var s=oin.value;
    if (str.indexof (s) == -1) {
      alert ("No such number");
      return false
    }
    var m="&span>" + s + "</span>";
    str=str.split (s);
    h=str.join (m)
    document.body.innerhtml=h
  }
  • Previous jquery implementation interface no refresh loading login registration
  • Next Object oriented Javascript OOP