What is the string object in TypeScript?

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.

Syntax

let _name = new String(string);
// or 
let varname:string = "somevalue"; 

Template string

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.

Methods

The string object can be manipulated with methods, just like most objects. A few of these methods are as follows:

  1. charAt(): Returns the character at the specified index.

  2. charCodeAt(): Returns a numerical value for the Unicode value of the character at a given index.

  3. concat(): Combines the text of two strings and returns a new string.

  4. slice(): Extracts a section of a string and returns a new string.

  5. substr(): Returns the characters in a string, beginning at the specified location through the specified number of characters.

  6. toUpperCase(): Returns the calling string value converted to uppercase letters.

  7. replace(): Used to find a match between a regular expression and a string and to replace the matched substring with a new substring.

Free Resources