Home » TypeScript String

TypeScript String

In TypeScript, the string is an object which represents the sequence of character values. It is a primitive data type which is used to store text data. The string values are surrounded by single quotation mark or double quotation mark. An array of characters works the same as a string.

Syntax

Example

Output:

Message: Hello tutoraspire  Length: 16  

There are three ways in which we can create a string.

1. Single quoted strings

It enclosed the string in a single quotation mark, which is given below.

Example

2. Double quoted strings

It enclosed the string in double quotation marks, which is given below.

Example

3. Back-ticks strings

It is used to write an expression. We can use it to embed the expressions inside the string. It is also known as Template string. TypeScript supports Template string from ES6 version.

Example

Output:

Before ES6: Rohit Sharma works in the tutoraspire company.  After ES6: Rohit Sharma works in the tutoraspire company.  

Multi-Line String

ES6 provides us to write the multi-line string. We can understand it from the below example.

Example

If we want that each line in the string contains “new line” characters, then we have to add “n” at the end of each string.

Example

Output:

hello  tutoraspire  my  name  is  Rohit Sharma  

String Literal Type

A string literal is a sequence of characters enclosed in double quotation marks (” “). It is used to represent a sequence of character which forms a null-terminated string. It allows us to specify the exact string value specified in the “string literal type.” It uses “pipe” or ” | “ symbol between different string value.

Syntax

String literal can be used in two ways-

1. Variable Assignment

We can assign only allowed values to a literal type variable. Otherwise, it will give the compile-time error.

Example

Output:

Correct  compilation error  

2. Function Parameter

We can pass only defined values to literal type argument. Otherwise, it will give the compile-time error.

Example

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

Output:

Combined Result: Hellotutoraspire  Character At 4: T  Index of T: 4  After Replacement: Welcome to  UpperCase: tutoraspire  
Next TopicTypeScript Numbers

You may also like