First create a string. There are two ways to create a string:
1. Literal: var str = "abc123efg456";
2. Constructor: var str = newString("abc123efg456");
Secondly, it should be noted that JavaScript strings are immutable, and none of the methods defined by the String class can change the string content. If you need to change the content of the string through a method, you need to reassign the return value to the original string. For example: str = str.toUpperCase();
Below, we introduce the more commonly used string methods:
var str = "1abc123abc456abc";
Function :Returns the first occurrence of the specified string value in the string.
Parameters: s is the character to be retrieved, required; n is an optional character parameter, specifying the position of the string retrieval.
demo:
var index1 = str.indexOf("a");
var index2 =str.indexOf("a",8);< /p>
var index3 =str.indexOf("a",14);
console.log(index1); // 1
console.log(index2) ; // 13
console.log(index3); // -1? Unable to retrieve characters from the retrieved position, return -1
Function: Return a specified string The last occurrence of a value, searched from back to front at the specified position in a string.
Parameters: s is the character to be retrieved, required; n is an optional character parameter, specifying the position of the string retrieval.
demo:
var index1 =str.lastIndexOf("a");?
var index2 =str.lastIndexOf("a",11);
var index3 =str.lastIndexOf("a",0);
console.log(index1); // 13
console.log(index2 ); // 7
console.log(index3); // -1? No character can be retrieved from the retrieved position, and -1 is returned
Function: Returns the character at the specified position .
Parameter: n represents the index of a certain position in the string. If not written, it defaults to the 0th character.
demo:
var s = str.charAt(3);
console.log(s);? // c
Function: Returns the Unicode encoding of the character at the specified position.
Parameter: n represents the index of a certain position in the string. There can be no parameters. The default is the parameter with index 0.
demo:
var s = str.charCodeAt(3);
console.log(s);? // 99
Note: The unicode encoding of c is 99.
Note: The charAt(n) method is similar to the charCodeAt(n) method. The former returns characters, and the latter returns the unicode encoding of characters.
Function: Convert encoding to character method.
Parameters: decimal and hexadecimal, unicode encoding is not supported.
demo:
var s1 =String.fromCharCode("0x56fd");
var s2 = String.fromCharCode(22269);
< p> console.log(s1);? //Countryconsole.log(s2);? //Country
Note: The hexadecimal encoding of "country" is 56fd, decimal is 22269.
Function: intercept the substring according to the specified position, from m to n, excluding n.
Parameters: m, n is the index.
demo:
var s = str.slice(2,7);
console.log(s); // bc123
< p> Function: According to the specified position, intercept the substring from m to n, excluding n.Parameters: m, n is the index.
demo:
var s = str.substring(2,7);
console.log(s); // bc123
< p> Function: According to the specified position, intercept the substring, take it from the m position, and finally take n.Parameters: m, n is the index.
demo:
var s = str.substr(2,7);
console.log(s); // bc123ab
< p> Function: String replacement.Parameters: oldstr is the character that needs to be replaced, and newstr is the character to be replaced.
demo:
var s =str.replace("a","l");
console.log(s); // 1lbc123abc456abc< /p>
Note: Only one qualified character can be replaced at a time.
Function: Split characters and return them as arrays.
Parameter: "s" is the character to separate.
demo1:
var arr1 = str.split("b");
console.log(arr1);? // ["1a", "c123a","c456a", "c"]
demo2:
var arr2 = str.split(); //["1abc123abc456abc"]
console.log(arr2);
demo3:
var arr3 = str.split("");
console.log(arr3); / / ["1","a", "b", "c", "1", "2","3", "a", "b", "c", "4","5", "6", "a", "b", "c"]
Note: When the parameter is not written, an array composed of the original string is returned; when the parameter is a null character, each character Split, return an array
Function: Retrieve the specified substring in the string and return the first index value that matches the string.
Parameter: The string that needs to be retrieved.
demo:
var s1= str.search("abc");
var s2 = str.search("efg");
console.log(s1); // 1
console.log(s2); // -1
Note: If no matching substring is found, Returns -1.
Function: Search the specified value within the string, and return the specified character value if it matches.
Parameter: The string that needs to be retrieved.
demo:
var s1= str.match("abc");
var s2 = str.match("efg");
console.log(s1); // ["abc",index: 1, input: "1abc123abc456abc", groups: undefined]
console.log(s2); // null< /p>
Note: This method is similar to indexOf() and lastIndexOf(), but it returns the specified value, otherwise it returns null
Function: Connect two or more strings and return the connection the string after.
Parameter: The string to be connected.
demo:
var str1="abc ";
var str2="123";
console.log(str1. concat(str2));? // "abc 123"
Tip: If you need to concatenate strings, it is easier to use the operator "+".
Function: Convert string to lowercase.
Parameters: None
demo:
var str = "ABC abc";
console.log(str.toLowerCase() ); // abcabc
Function: Convert string to uppercase.
Parameters: None
demo:
var str = "ABC abc";
console.log(str.toUpperCase() ); // ABCABC
Function: Display the string in large font.
Parameters: None
demo:
var s = str.big();
document.write(str+"") ;
document.write(s);
Function: Display the string as a small font.
Parameters: None
demo:
var s = str.small();
document.write(str+"") ;
document.write(s);
Function: Display the string in bold.
Parameters: None
demo:
var s = str.bold();
document.write(str+"") ;
document.write(s);
Function: Display the string in italics.
Parameters: None
demo:
var s = str.italics();
document.write(str+"") ;
document.write(s);
Function: Display characters in the specified color.
Parameters: color name (red), RGB value (rgb(255,0,0)) or hexadecimal number (#FF0000).
demo:
var s = str.fontcolor("red");
document.write(str+"");
document.write(s);
Function: Display characters in the specified font size.
Parameters: The size parameter must be a number between 1 and 7, 1 represents a small font size, and 7 represents the largest font size.
demo:
var s = str.fontsize(7);
document.write(str+"");
document .write(s);
Function: Display the string as a hyperlink.
Parameters: URL of the link.
demo:
var s =str.link("");
document.write(str+"");
document.write(s);
Function: Add string to string for display.
Parameters: None
demo:
var s = str.strike();
document.write(str+"") ;
document.write(s);
The above is a summary of string methods. If there are any modifications or additions, it will be updated later.
If there are any omissions, errors, unreasonableness, unclear descriptions, inaccuracies, etc. in the article, you are welcome to leave a message to correct me...