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.
let var_name = new String(string);
let uname = new String("Hello rookienerd"); console.log("Message: " +uname); console.log("Length: "+uname.length);
There are three ways in which we can create a string.
It enclosed the string in a single quotation mark, which is given below.
var studentName: String = 'Peter';
It enclosed the string in double quotation marks, which is given below.
var studentName: String = "Peter";
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.
let empName:string = "Rohit Sharma"; let compName:string = "rookienerd"; // 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);
ES6 provides us to write the multi-line string. We can understand it from the below example.
let multi = 'hello ' + 'world ' + 'my ' + 'name ' + 'is ' + 'Rohit';
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.
let multi = ' hello\n ' + 'rookienerd\n ' + 'my\n ' + 'name\n ' + 'is\n ' + 'Rohit Sharma'; console.log(multi);
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.
Type variableName = "value1" | "value2" | "value3"; // upto N number of values
String literal can be used in two ways-
We can assign only allowed values to a literal type variable. Otherwise, it will give the compile-time error.
type Pet = 'cat' | 'dog' | 'Rabbit'; let pet: Pet; if(pet = 'cat'){ console.log("Correct"); }; if(pet = 'Deer') { console.log("compilation error"); };
We can pass only defined values to literal type argument. Otherwise, it will give the compile-time error.
type FruitsName = "Apple" | "Mango" | "Orange"; function showFruitName(fruitsName: FruitsName): void { console.log(fruitsName); } showFruitName('Mango'); //OK - Print 'Mango' //Compile Time Error showFruitName('Banana');
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. |
//String Initialization let str1: string = 'Hello'; let str2: string = 'rookienerd'; //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());