📜  JavaScript-字符串对象

📅  最后修改于: 2020-12-18 05:02:18             🧑  作者: Mango


String对象使您可以处理一系列字符;它使用许多辅助方法包装Javascript的字符串原始数据类型。

由于JavaScript字符串之间的原语自动转换和字符串对象,你可以调用任何String对象的一个字符串原始的辅助方法。

句法

使用以下语法创建String对象-

var val = new String(string);

String参数是一系列已正确编码的字符。

字符串属性

这是String对象的属性及其描述的列表。

Sr.No. Property & Description
1 constructor

Returns a reference to the String function that created the object.

2 length

Returns the length of the string.

3 prototype

The prototype property allows you to add properties and methods to an object.

在以下各节中,我们将通过一些示例来演示String属性的用法。

字符串方法

这是String对象中可用方法的列表以及它们的描述。

Sr.No. Method & Description
1 charAt()

Returns the character at the specified index.

2 charCodeAt()

Returns a number indicating the Unicode value of the character at the given index.

3 concat()

Combines the text of two strings and returns a new string.

4 indexOf()

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

5 lastIndexOf()

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

6 localeCompare()

Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

7 match()

Used to match a regular expression against a string.

8 replace()

Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

9 search()

Executes the search for a match between a regular expression and a specified string.

10 slice()

Extracts a section of a string and returns a new string.

11 split()

Splits a String object into an array of strings by separating the string into substrings.

12 substr()

Returns the characters in a string beginning at the specified location through the specified number of characters.

13 substring()

Returns the characters in a string between two indexes into the string.

14 toLocaleLowerCase()

The characters within a string are converted to lower case while respecting the current locale.

15 toLocaleUpperCase()

The characters within a string are converted to upper case while respecting the current locale.

16 toLowerCase()

Returns the calling string value converted to lower case.

17 toString()

Returns a string representing the specified object.

18 toUpperCase()

Returns the calling string value converted to uppercase.

19 valueOf()

Returns the primitive value of the specified object.

字符串HTML包装器

这是方法的列表,这些方法返回包装在适当的HTML标记中的字符串的副本。

Sr.No. Method & Description
1 anchor()

Creates an HTML anchor that is used as a hypertext target.

2 big()

Creates a string to be displayed in a big font as if it were in a tag.

3 blink()

Creates a string to blink as if it were in a tag.

4 bold()

Creates a string to be displayed as bold as if it were in a tag.

5 fixed()

Causes a string to be displayed in fixed-pitch font as if it were in a tag

6 fontcolor()

Causes a string to be displayed in the specified color as if it were in a tag.

7 fontsize()

Causes a string to be displayed in the specified font size as if it were in a tag.

8 italics()

Causes a string to be italic, as if it were in an tag.

9 link()

Creates an HTML hypertext link that requests another URL.

10 small()

Causes a string to be displayed in a small font, as if it were in a tag.

11 strike()

Causes a string to be displayed as struck-out text, as if it were in a tag.

12 sub()

Causes a string to be displayed as a subscript, as if it were in a tag

13 sup()

Causes a string to be displayed as a superscript, as if it were in a tag

在以下各节中,我们将通过一些示例来演示String方法的用法。