Javatpoint Logo
Javatpoint Logo

TypeScript String

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

Syntax

Example

Output:

Message: Hello JavaTpoint
Length: 16

Template String

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

Example

Output:

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

Multi-Line String

ES6 provides us to write the multi-line string. This can be shown in 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
JavaTpoint
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.

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, a string literal allow us to specify the exact string value specified in the string literal type. A string literal type uses "pipe" or " | " symbol between different allowed 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 includes 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 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: HelloJavaTpoint
Character At 4: T
Index of T: 4
After Replacement: Welcome to
UpperCase: JAVATPOINT
Next TopicTypeScript Numbers




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


B.Tech / MCA