In TypeScript, string
is a data type that represents the sequence of character values. string
is a primitive data type that is used to store textual data.
string
values are surrounded by singular quotation marks, ''
, or double quotation marks, ""
. An array of characters works the same as a string.
let _name = new String(string);
// or
let varname:string = "somevalue";
After the introduction of ES6, strings
can be written between backticks (``) instead of single or double-quotes. The values of the variable can be accessed using ${}
as part of the string itself.
The string
object can be manipulated with methods, just like most objects. A few of these methods are as follows:
charAt()
:
Returns the character at the specified index.
charCodeAt()
:
Returns a numerical value for the Unicode value of the character at a given index.
concat()
:
Combines the text of two strings and returns a new string.
slice()
:
Extracts a section of a string and returns a new string.
substr()
:
Returns the characters in a string, beginning at the specified location through the specified number of characters.
toUpperCase()
:
Returns the calling string value converted to uppercase letters.
replace()
:
Used to find a match between a regular expression and a string and to replace the matched substring with a new substring.