Home > Web Front-end > JS Tutorial > How to use string method in javascript

How to use string method in javascript

coldplay.xixi
Release: 2021-04-09 15:22:00
Original
3712 people have browsed it

The use of string methods in JavaScript: 1. The length attribute of the String object declares the number of characters in the string; 2. The String class defines a large number of methods for operating strings.

How to use string method in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

Usage of string method in JavaScript:

String object description

String is a basic data in JavaScript type.

The length property of the String object declares the number of characters in the string.

The String class defines a large number of methods for manipulating strings, such as extracting characters or substrings from strings, or retrieving characters or substrings.

It should be noted that JavaScript strings are immutable, and none of the methods defined by the String class can change the content of the string. Methods like String.toUpperCase() return a completely new string instead of modifying the original string.

In older JavaScript implementations of the Netscape code base (such as the Firefox implementation), strings behaved like read-only character arrays. For example, to extract the third character from the string s, you can use s[2] instead of the more standard s.charAt(2). Additionally, when a for/in loop is applied to a string, it will enumerate the array index for each character in the string (but note that the ECMAScript standard states that the length property cannot be enumerated). Because the behavior of arrays of strings is nonstandard, its use should be avoided.

String object

String object is used to process text (string).

The syntax for creating a String object:

new String(s);
String(s);
Copy after login

Parameters

The parameters s are to be stored in the String object or converted to The value of the original string.

Return value

When String() is used as a constructor together with operator new, it returns a newly created String object, which stores the string s or a string representation of s.

When String() is called without the new operator, it simply converts s to the original string and returns the converted value.

Related free learning recommendations: javascript learning tutorial

The above is the detailed content of How to use string method in javascript. 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