Home > Web Front-end > JS Tutorial > Detailed explanation of JavaScript string processing (String object)_Basic knowledge

Detailed explanation of JavaScript string processing (String object)_Basic knowledge

WBOY
Release: 2016-05-16 16:33:22
Original
1473 people have browsed it

Define String object

JavaScript String object is used to process text strings. The syntax for creating a String object is as follows:

Copy code The code is as follows:



Among the above three methods, only the first one uses the String constructor to strictly define a string object and returns an object. The second is to call the String function to convert the parameter str to the original string string and return it. The third method is to define a string variable, but it is still treated as a string object in JavaScript.

Run the following statements to know the difference:

Copy code The code is as follows:

alert( typeof str_object ); // Output object
alert( typeof str1 ); // Output string
alert( typeof str2 ); // Output string

String object properties

属性 描述
constructor 对创建该对象的函数的引用
length 字符串的长度
prototype 向对象添加属性和方法

String object method

The list of common methods of String object is as follows:

String output

alert(): Message warning box output text
document.write(): Output text to the Web page

String operations

concat(): Concatenate two or more strings
replace(): String replacement or regular match replacement
slice(): intercept a string by specifying the start and end positions
split(): Split the string into a string array
substr(): intercept the string according to the starting position and length
substring(): intercept the string

by specifying the start and end positions

String conversion

toLowerCase(): Convert the string to lowercase
toUpperCase(): Convert the string to uppercase
fromCharCode(): Convert one or more Unicode values ​​to a string

String search

charAt(): Get the character at the specified position
charCodeAt(): Get the Unicode encoding of the character at the specified position
indexOf(): Calculate the position where a specified string first appears in the string
lastIndexOf(): Calculate the position where a specified string first appears in the string

HTML tag class

bold(): Get the character at the specified position
fontcolor(): Display the string according to the specified color
fontsize(): Display the string according to the specified size
italics(): Display the string in italics
link(): Add a hyperlink to a string
strike(): Add strikethrough to the string
sub(): Display the string as a subscript
sup(): Display the string as superscript

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