This page is a work in progress.You can help improve it. →

Strings

TypeScript's String type is used to represent textual data. It is a set of "elements." Each element in the String occupies a position in the String. The first element is at index 0, the next at index 1, and so on. The length of a String is the number of elements in it. You can create strings using string literals or string objects.

String literals

You can create simple strings using either single or double quotes:

'foo'
"bar"

Commonly used methods of strings

MethodDescription
indexOf, lastIndexOfReturn the position of specified substring in the string or last position of specified substring, respectively.
startsWith, endsWith, includesReturns whether or not the string starts, ends or contains a specified string.
concatCombines the text of two strings and returns a new string.
splitSplits a String object into an array of strings by separating the string into substrings.
sliceExtracts a section of a string and returns a new string.
substring, substrReturn the specified subset of the string, either by specifying the start and end indexes or the start index and a length.
match, matchAll, replace, searchWork with regular expressions.
toLowerCase, toUpperCaseReturn the string in all lowercase or all uppercase, respectively.

Less commonly used methods of strings

MethodDescription
charAt, charCodeAt, codePointAtReturn the character or character code at the specified position in string.
fromCharCode, fromCodePointConstructs a string from the specified sequence of Unicode values. This is a method of the String class, not a String instance.
splitSplits a String object into an array of strings by separating the string into substrings.
sliceExtracts a section of a string and returns a new string.
substring, substrReturn the specified subset of the string, either by specifying the start and end indexes or the start index and a length.
match, matchAll, replace, searchWork with regular expressions.
toLowerCase, toUpperCaseReturn the string in all lowercase or all uppercase, respectively.
repeatReturns a string consisting of the elements of the object repeated the given times.
trimTrims whitespace from the beginning and end of the string.
© 2017 - 2022; Built with ♥ in St. Petersburg, Florida.