JS String Object


String object

String objects are used to work with text (strings).

String object creation method: new String() .

Grammar

var txt = new String("string");

Or a simpler way:

var txt = "string";

For the String object tutorial, check out JavaScript String Object Tutorial .


String object properties

PropertyDescription
constructorReference to the function that created the object
lengthThe length of the string
prototypeAllows you to add properties and methods to objects

String object methods

MethodDescription
charAt()Returns the characters in the specified position.
charCodeAt()Returns the Unicode encoding of the characters in the specified position.
concat()Concats 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 characters.
indexOf()Returns the position where a specified string value first appeared in the string.
includes()Looks for whether the specified substring is included in the string.
lastIndexOf()Searches for strings from back to front and calculates the position where the string last appears starting from the start position (0).
match()Finds a match for one or more regular expressions.
repeat()Copies the string a specified number of times and returns them concatenated together.
replace()Finds the matching substring in the string and replaces the substring that matches the regular expression.
replaceAll()Finds matching substrings in the string and replaces all substrings that match the regular expression.
search()Looks for a value that matches a regular expression.
slice()Extracts a fragment of a string and returns the extracted part in a new string.
split()Splits a string into an array of strings.
startsWith()Checks whether the string begins with the specified substring.
substr()Extracts the specified number of characters from the string from the starting index number.
substring()Extracts the characters between two specified index numbers in a string.
toLowerCase()Converts a string to lowercase.
toUpperCase()Converts a string to uppercase.
trim()Strips away white space on either side of the string.
toLocaleLowerCase()

Converts strings to lowercase based on the localhost's locale

toLocaleUpperCase()Converts strings to uppercase based on the localhost's locale.
valueOf()Returns the original value of a string object.
toString()Returns a string.

String HTML wrapper method

HTML returns the content contained in the corresponding HTML tag.

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

MethodDescription
anchor()Create HTML anchors.
big()Display strings in large font.
blink()A flashing string is displayed.
bold()Strings are displayed in bold.
fixed()Display the string in typewriter text.
fontcolor()Displays the string using the specified color.
fontsize()Displays the string using the specified dimensions.
italics()Display strings in italics.
link()Display strings as links.
small()Use small font size to display strings.
strike()String to display strikethrough.
sub()Display strings as subscripts.
sup()Display strings as superscripts.