Open In App

TypeScript String

Last Updated : 16 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In TypeScript, the string is sequence of char values and also considered as an object. It is a type of primitive data type that is used to store text data. The string values are used between single quotation marks or double quotation marks, and also array of characters works same as a string. TypeScript string work with the series of character.

Syntax

var var_name = new String(string); 

Property

  • Constructor: It will return a reference to the string function.
  • Length: This property will return the length of the string.
  • Prototype: This property let you add the property and methods.

Methods of TypeScript String

MethodDescription
charAt()Returns the character at the specified index.
charCodeAt()Returns a number indicating the Unicode value of the character at the specified index.
concat()Combines two separate strings and returns the combined string.
indexOf()Returns the index of the first occurrence of the specified value, or -1 if not found.
lastIndexOf()Returns the index of the last occurrence of the specified value, or -1 if not found.
localeCompare()Returns a number indicating whether the reference string comes before, after, or is the same as the given string in sort order.
match()Matches a regular expression with the specified string and returns the matches.
replace()Replaces matched strings based on a regular expression with a new substring.
search()Searches for a match between a regular expression and the specified string.
slice()Extracts a section of a string and returns it as a new string.
split()Splits a string into an array of substrings based on a specified delimiter.
substr()Returns a substring starting from a specified index and extending for a given number of characters.
substring()Returns a substring between two specified indices.
toLocaleLowerCase()Converts the string to lowercase according to the host’s current locale.
toLocaleUpperCase()Converts the string to uppercase according to the host’s current locale.
toLowerCase()Converts the string to lowercase.
toString()Returns a string representing the specified object.
toUpperCase()Converts the string to uppercase.
valueOf()Returns the primitive value of the specified string.

Example:

let uname = new String("Hello geeksforgeeks");  
console.log("Message: " + uname);  
console.log("Length: " + uname.length);

Output:

Message: Hello geeksforgeeks
Length: 19

Template String:

Template string support in TypeScript from ES6 version. Template strings are used to fixed the expressions into strings.

Example:

let emplName:string =  "Mohit Jain";   
let compName:string = "geeksforgeeks";   
// Pre-ES6  
let emplDetail1: string = emplName + " works in the " + compName + " company.";   
// Post-ES6  
let emplDetail2: string = `${emplName} works in the ${compName} company.`;   
console.log("Before ES6: " +emplDetail1);  
console.log("After ES6: " +emplDetail2);

Output:

Before ES6: Mohit Jain works in the geeksforgeeks company.
After ES6:  Mohit Jain works in the geeksforgeeks company.

Multi-Line String:

ES6 provides us to write the multi-line string. This can be shown in the below example. We have to add “\n” at the end of each string for containing a new line in the string. This represents the clear cut statement.

Example:

let multi = ' hello\n ' +  
    'Geeksforgeeks\n ' +  
    'my\n ' +  
    'name\n ' +  
    'is\n ' +  
    'Mohit Jain';  
console.log(multi);  

Output:

hello
Geeksforgeeks
my
name
is
Mohit Jain

String Literal Type

It is also a type of string and strings literal is a sequence of characters enclosed in double quotation marks (” “) and terminated with a null value. In a string literal type is a type whose expected value is a string with textual contents equal to that of the string literal type. In other words, we can say a string literal allow us to specify the exact string value specified in the string literal type. While a string literal type uses different allowed string value then use “pipe” or ” | ” symbol between them.

Syntax:

Type variableName = "value1" | "value2" | "value3"; // upto N number of values  

String literal can be used in two ways:

Variable Assignment:

Whatever the values will be allowed for literal type variable then We can assign. Otherwise, it will give the compile-time error. Example:

type Pet = 'mouse' | 'dog' | 'Rabbit';  
let pet: Pet;  
if(pet = 'mouse'){  
    console.log("Correct");  
};  
if(pet = 'Deer')  
{  
    console.log(" we got compilation error type!");  
}; 
Output:
Correct
we got compilation error type!

Function Parameter:

For literal type argument we can pass only defined values. Otherwise, it will give the compile-time error. Example:

type FruitsName = "Apple" | "Mango" | "Orange";  
function showFruitName(fruitsName: FruitsName): void {  
    console.log(fruitsName);  
}  
showFruitName('Mango');   //OK - Print 'Mango'  
//Compile Time Error  
showFruitName('Banana');
Output:
Mango
Banana



Next Article

Similar Reads

three90RightbarBannerImg