This time I will bring you the properties and methods of common strings in JS. What are the precautions when using string properties and methods in JS? The following is a practical case. Let’s take a look.
Attribute
length: Returns the length of the string
var str='hello world'; alert(str.length); // 11
Method
charAt(): Returns the character at the specified index position
var str='hello world'; alert(str.charAt(4)); // o
charCodeAt(): Returns the Unicode encoding of the character at the specified index position
var str='a'; alert(str.charCodeAt(0)); // 97
fromCharCode(): Converts the Unicode encoding to a string
alert(String.fromCharCode(97)); // a
concat(): Concatenates two or more string, returns the concatenated string
var str1='hello';var str2=' world'; alert(str1.concat(str2)); // hello world
indexOf(): Returns the position where the specified string first appears, no -1 is returned
var str='hello world,hello moli'; alert(str.indexOf('hello')); // 0
lastIndexOf(): Returns the specified character The position of the last occurrence of the string, no return -1
var str='hello world,hello moli'; alert(str.lastIndexOf('hello')); // 12
match(): Find one or more matches of regular expression, no return null
var str='hello world,hello moli'; alert(str.match('hello')); // hello
replace (): Replace the substring that matches the regular expression (default only replaces the first matching substring, add g to replace all matching substrings)
var str='hello world';// 用moli替换worldalert(str.replace(/world/,'moli')); // hello moli
search: Returns the substring that matches the regular expression The starting position of the substring, no return -1
var str='hello world'; alert(str.search(/world/)); // 6
slice(): returns the specified starting position (including the starting position, if it is a negative number, the starting position is calculated from the end, that is, -1 means the penultimate a) to the specified end position (excluding the end position, if this parameter is not specified, it includes all characters from the specified start position to the end of the string)
var str='hello world'; alert(str.slice(6,11)); // world
split(): The string is split into an array of substrings (the second parameter can specify the maximum length of the returned array, optional)
var str='h-e-l-l-o'; alert(str.split('-')); // h,e,l,l,o
substr(index,length): Extract from the specified index (index, required, if If a negative number is used, the starting position is calculated from the end, that is, -1 indicates the length starting from the last one) (length, optional, if this parameter is not specified, it includes all characters from the specified index to the end of the string) characters
var str='hello world,hello moli'; alert(str.substr(5,6)); // world
substring(): Extract the specified start position (including the start position) to the end position (excluding the end position, optional, if this parameter is not specified, it includes the characters from the specified start position to the end position) characters at the end of the string)
var str='hello moli';alert(str.substring(6,8)); // mo// 注:// 与 slice() 和 substr() 方法不同的是,substring() 不接受负的参数
toLowerCase(): Convert the string to lowercase
var str='Hello Moli'; alert(str.toLowerCase()); // hello moli
toUpperCase(): Convert the string to uppercase
var str='Hello Moli'; alert(str.toUpperCase()); // HELLO MOLI
toString(): Return a string (omitted)
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Inheritance and prototype chain of JavaScript
Front-end framework management
The above is the detailed content of Common properties and methods of strings in JS. For more information, please follow other related articles on the PHP Chinese website!