Summary of String Methods for JavaScript
- 2021-07-06 09:58:10
- OfStack
1. stringObject.charAt()
Function: Returns the subscript of a string
var str=" This is 1 String string ";
console.log(str.charAt(0))// This
2. stringObject.charCodeAt()
Function: The method returns the Unicode encoding of the character at the specified position
var str=" This is 1 String string ";
console.log(str.charCodeAt(0))
// This =>36825
3.String.fromCharCode()
Function: Return the corresponding character through Unicode encoding
console.log(String.fromCharCode(36825,26159))// This is
Example: Find if 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 +' It's all numbers ' );
} else {
alert(' Input error ');
}
};
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: The method returns the first occurrence of a specified string value in the string.
Parameter: str. indexOf (find value, start looking for subscript), which returns-1 if the string value to retrieve does not appear.
Example: Return to find the subscript of the corresponding character
<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()
Function: Find the first occurrence of a specified string value in the string from back to front
6. stringObject.substring()
Function: The method is used to extract the characters between two specified subscripts in a string.
7. stringObject.toUpperCase()
Function: Letters are converted to uppercase
8. stringObject.toLowerCase()
Function: Letters are converted to lowercase
9.stringObject.split()
Function: The method is used to divide a string into a string array
Parameter: (by what character to truncate and to what bit to keep 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 separator
Two usages
var arr = [1,2,3];
arr.join("")//123
arr.join("-")//1-2-3
Example: Highlight the keywords you are looking for
<input type="text" id="oin" />
<button> Button </button>
var oin = document.getElementById("oin");
var obtn = document.getElementsByTagName('button')[0];
var str = "arguments The length of an object is determined by the number of arguments, not the number of formal arguments.
A formal parameter is a variable that re-opens memory space within a function, but it is the same as arguments Object
Memory space does not overlap. For arguments And values exist, the two values are synchronized
, but for one of the 1 If there is no value, the values will not be synchronized for this no value case.
The following code can be verified. ";
var h = "";
obtn.onclick = function() {
if (oin.value == "") {
alert(" Enter blank ");
return false
}
var s = oin.value;
if (str.indexOf(s) == -1) {
alert(" There is no such number ");
return false
}
var m = '<span style="background-color:red">' + s + '</span>';
str = str.split(s);
h = str.join(m)
document.body.innerHTML=h
}