JavaScript StringObject


String Object

The String object is used to handle text (strings).

How to create a String object:new String()。

Syntax

var txt = new String("string");

Or in a simpler way:

var txt = "string";

To learn about the String Object tutorial, please checkJavaScript String Object Tutorial。


String Object Properties

Property Description
constructor A reference to the function that created the object.
length The length of the string
prototype Allows you to add properties and methods to an object.

String Object Methods

Method Description
charAt() Returns the character at the specified position.
charCodeAt() Returns the Unicode encoding of the character at the specified position.
concat() Concatenates two or more strings and returns a new string.
endsWith() Determines whether the current string ends with the specified substring (case-sensitive).
fromCharCode() Converts Unicode encoding to a character.
indexOf() Returns the position of the first occurrence of a specified string value in a string.
includes() Determines whether a string contains the specified substring.
lastIndexOf() Searches the string from back to front, and returns the position of the last occurrence starting from the beginning position (0).
match() Finds one or more matches of a regular expression.
repeat() Copies the string a specified number of times and returns them concatenated together.
replace() Finds a matching substring in a string and replaces the substring that matches the regular expression.
replaceAll() Finds matching substrings in a string and replaces all substrings that match the regular expression.
search() Finds a value that matches a regular expression.
slice() Extracts a part of the string and returns the extracted portion in a new string.
split() Splits the string into an array of strings.
startsWith() Checks whether the string starts with the specified substring.
substr() Extracts a specified number of characters from a starting index in the string.
substring() Extracts characters between two specified index numbers in the string.
toLowerCase() Converts the string to lowercase.
toUpperCase() Converts the string to uppercase.
trim() Removes whitespace from both sides of the string.
toLocaleLowerCase() Converts the string to lowercase according to the local host's language environment.
toLocaleUpperCase() Converts the string to uppercase according to the local host's language environment.
valueOf() Returns the primitive value of a string object.
toString() Returns a string.

String HTML Wrapper Methods

HTML returns content wrapped in corresponding HTML tags.

The following methods are not standard methods, so they may not be supported in some browsers.

Method Description
anchor() Creates an HTML anchor.
big() Displays the string in large font.
blink() Displays the string as blinking text.
bold() Displays the string in bold.
fixed() Displays the string in typewriter text.
fontcolor() Displays the string using the specified color.
fontsize() Displays the string using the specified size.
italics() Displays the string in italic.
link() Displays the string as a link.
small() Displays the string in small font.
strike() Used to display a string with strikethrough.
sub() Displays the string as a subscript.
sup() Displays the string as a superscript.
Other Extensions