This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

String

Summary

Allows manipulation and formatting of text strings and determination and location of substrings within strings.

Syntax

// Declaring a string literal.
var newString = "stringLiteral";

// Or, declaring a String object - discouraged, long, verbose and pretty useless.
var newString = new String([" stringLiteral "]);
newString
Required. The variable name to which the String object is assigned.
stringLiteral
Optional. Any group of Unicode characters.

Examples

JavaScript provides escape sequences that you can include in strings to create characters that you cannot type directly. For example, \t specifies a tab character. For more information, see Special Characters (JScript).

A string literal is zero or more characters enclosed in single or double quotation marks. A string literal has a primary (primitive) data type of string. A String object is created by using the new Operator , and has a data type of Object.

The following example shows that the data type of a string literal is not the same as that of a String object.

var strLit = "This is a string literal.";
var strObj = new String("This is a string object.");

document.write(typeof strLit);
document.write("<br/>");
document.write(typeof strObj);
// Output:
// string
// object

When you call a method on a string literal, it is temporarily converted to a string wrapper object. The string literal is treated as though the new operator were used to create it.

The following example applies the toUpperCase method to a string literal.

var strLit = "This is a string literal.";

var result1 = strLit.toUpperCase();

var result2 = (new String(strLit)).toUpperCase();

document.write(result1);
document.write("<br/>");
document.write(result2);
// Output:
// THIS IS A STRING LITERAL.
// THIS IS A STRING LITERAL.

In modern browsers (2011 onwards), you can access an individual character of a string as a read-only array-indexed property. The following example accesses individual string characters.

var str = "abcd";
var result = str[2];
document.write (result);
// Output: c

var result = "the"[0];
document.write(result);
// Output: t

Properties

The following table lists the properties of the String object.

PropertyDescription
constructorSpecifies the function that creates an object.
lengthReturns the length of a String object.
prototypeReturns a reference to the prototype for a class of objects.

Functions

The following table lists the functions of the String object.

FunctionDescription
fromCharCodeReturns a string from a number of Unicode character values.

Methods

The following table lists the methods of the String object.

MethodDescription
HTML Tag MethodsPlaces various HTML tags around text.
charAtReturns the character at the specified index.
charCodeAtReturns the Unicode encoding of the specified character.
concatReturns a string that contains the concatenation of two supplied strings.
indexOfReturns the character position where the first occurrence of a substring occurs within a string.
lastIndexOfReturns the last occurrence of a substring within a string.
localeCompareReturns a value that indicates whether two strings are equivalent in the current locale.
matchSearches a string by using a supplied Regular Expression object and returns the results as an array.
replaceUses a regular expression to replace text in a string and returns the result.
searchReturns the position of the first substring match in a regular expression search.
sliceReturns a section of a string.
splitReturns the array of strings that results when a string is separated into substrings.
substrReturns a substring beginning at a specified location and having a specified length.
substringReturns the substring at a specified location within a String object.
toLocaleLowerCaseReturns a string in which all alphabetic characters are converted to lowercase, taking into account the host environment’s current locale.
toLocaleUpperCaseReturns a string in which all alphabetic characters are converted to uppercase, taking into account the host environment’s current locale.
toLowerCaseReturns a string in which all alphabetic characters are converted to lowercase.
toStringReturns the string.
toUpperCaseReturns a string in which all alphabetic characters are converted to uppercase.
trimReturns a string with leading and trailing white space and line terminator characters removed.
valueOfReturns the string.

See also

Other articles

Attributions

  • Microsoft Developer Network: Article