Home > Web Front-end > JS Tutorial > body text

Common properties and methods of strings in JS

php中世界最好的语言
Release: 2018-03-20 10:48:37
Original
2180 people have browsed it

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
Copy after login

Method

charAt(): Returns the character at the specified index position

var str='hello world';
alert(str.charAt(4)); // o
Copy after login

charCodeAt(): Returns the Unicode encoding of the character at the specified index position

var str='a';
alert(str.charCodeAt(0)); // 97
Copy after login

fromCharCode(): Converts the Unicode encoding to a string

alert(String.fromCharCode(97)); // a
Copy after login

concat(): Concatenates two or more string, returns the concatenated string

var str1='hello';var str2=' world';
alert(str1.concat(str2)); // hello world
Copy after login

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
Copy after login

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
Copy after login

match(): Find one or more matches of regular expression, no return null

var str='hello world,hello moli';
alert(str.match('hello')); // hello
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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() 不接受负的参数
Copy after login

toLowerCase(): Convert the string to lowercase

var str='Hello Moli';
alert(str.toLowerCase()); // hello moli
Copy after login

toUpperCase(): Convert the string to uppercase

var str='Hello Moli';
alert(str.toUpperCase()); // HELLO MOLI
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!