TypeScript String

In TypeScript, a string is an object that signifies a series of character values. This primitive data type is utilized for storing textual information. String values can be enclosed in either single or double quotation marks. An array of characters functions similarly to a string.

Syntax

Example

let var_name = new String(string);

Example

Example

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

Output:

Output

Message: Hello TypeScript Tutorial
Length: 16

We can generate a string using three different methods.

1. Single quoted strings

It wrapped the string in a single quote, as shown below.

Example

Example

var studentName: String = 'Peter';

2. Double quoted strings

It encapsulated the string within double quotation marks, as shown below.

Example

Example

var studentName: String = "Peter";

3. Back-ticks strings

It is employed to construct an expression. This allows for the integration of expressions within a string. It is commonly referred to as a Template string. TypeScript incorporates Template strings starting from the ES6 version.

Example

Example

let empName:string = "Rohit Sharma"; 
let compName:string = "TypeScript Tutorial"; 
// Pre-ES6
let empDetail1: string = empName + " works in the " + compName + " company."; 
// Post-ES6
let empDetail2: string = `${empName} works in the ${compName} company.`; 
console.log("Before ES6: " +empDetail1);
console.log("After ES6: " +empDetail2);

Output:

Output

Before ES6: Rohit Sharma works in the TypeScript Tutorial company.
After ES6: Rohit Sharma works in the TypeScript Tutorial company.

Multi-Line String

ES6 enables us to create multi-line strings. This can be illustrated with the example below.

Example

Example

let multi = 'hello ' +
    'world ' +
    'my ' +
    'name ' +
    'is ' +
    'Rohit';

To ensure that every line in the string includes "new line" characters, it is necessary to append "\n" to the conclusion of each string.

Example

Example

let multi = ' hello\n ' +
    'TypeScript Tutorial\n ' +
    'my\n ' +
    'name\n ' +
    'is\n ' +
    'Rohit Sharma';
console.log(multi);

Output:

Output

hello
TypeScript Tutorial
my
name
is
Rohit Sharma

String Literal Type

A string literal consists of a series of characters that are surrounded by double quotation marks (" "). It serves to denote a sequence of characters that creates a null-terminated string. This enables the precise definition of the string value indicated in the "string literal type." The "pipe" or " | " symbol is utilized to separate various string values.

Syntax

Example

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

String literal can be used in two ways-

1. Variable Assignment

A literal type variable can only be assigned permitted values. Failing to do so will result in a compile-time error.

Example

Example

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

Output:

Output

Correct
compilation error

2. Function Parameter

Only explicitly defined values can be provided to a literal type argument. Failing to do so will result in a compile-time error.

Example

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:

Output

Mango
Banana

String Methods

The list of string methods with their description is given below.

SN Method Description
1. charAt() It returns the character of the given index.
2. concat() It returns the combined result of two or more string.
3. endsWith() It is used to check whether a string ends with another string.
4. includes() It checks whether the string contains another string or not.
5. indexOf() It returns the index of the first occurrence of the specified substring from a string, otherwise returns -1.
6. lastIndexOf() It returns the index of the last occurrence of a value in the string.
7. match() It is used to match a regular expression against the given string.
8. replace() It replaces the matched substring with the new substring.
9. search() It searches for a match between a regular expression and string.
10. slice() It returns a section of a string.
11. split() It splits the string into substrings and returns an array.
12. substring() It returns a string between the two given indexes.
13. toLowerCase() It converts the all characters of a string into lower case.
14. toUpperCase() It converts the all characters of a string into upper case.
15. trim() It is used to trims the white space from the beginning and end of the string.
16. trimLeft() It is used to trims the white space from the left side of the string.
17. trimRight() It is used to trims the white space from the right side of the string.
18. valueOf() It returns a primitive value of the specified object.

Example

Example

//String Initialization
let str1: string = 'Hello';
let str2: string = 'TypeScript Tutorial';
//String Concatenation
console.log("Combined Result: " +str1.concat(str2));
//String charAt
console.log("Character At 4: " +str2.charAt(4));
//String indexOf
console.log("Index of T: " +str2.indexOf('T'));
//String replace
console.log("After Replacement: " +str1.replace('Hello', 'Welcome to'));
//String uppercase
console.log("UpperCase: " +str2.toUpperCase());

Output:

Output

Combined Result: HelloTypeScript Tutorial
Character At 4: T
Index of T: 4
After Replacement: Welcome to
UpperCase: LOGIC PRACTICE

Input Required

This code uses input(). Please provide values below: