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.
Message: Hello JavaTpoint 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.
2. Double quoted strings
It enclosed the string in double quotation marks, which is given below.
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.
Before ES6: Rohit Sharma works in the JavaTpoint company. After ES6: Rohit Sharma works in the JavaTpoint company.
ES6 provides us to write the multi-line string. We can understand it from the below 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.
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. It allows us to specify the exact string value specified in the "string literal type." It uses "pipe" or " | " symbol between different string value.
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.
Correct compilation error
2. Function Parameter
We can pass only defined values to literal type argument. Otherwise, it will give the compile-time error.
The list of string methods with their description is given below.
Combined Result: HelloJavaTpoint Character At 4: T Index of T: 4 After Replacement: Welcome to UpperCase: JAVATPOINT