☰ See All Chapters |
TypeScript string type
Anything inside double quotes or single quotes is a string value in TypeScript. You can also create multiline string by putting everything inside backtick(`). Below is an example for different string values.
let message: string = "welcome to www.java4coding.com"; let helloMessage: string = 'Hello Dear Reader!'; let description: string = `TypeScript learning is must before Angular. Google adapted TypeScript language developed at Microsoft to Angular.` |
String Handling Methods
TypeScript supports string handling methods listed in the below table:
Method | Description |
charAt(num: number) | Returns the character present at the given index. |
charCodeAt(num: number) | Returns the Unicode value of the character present at the given index. |
concat(str1, str2, str3, ...) | Returns the concatenation of the two or more strings. |
fromCharCode(num: number) | Coverts Unicode value to string. |
indexOf(str: string) | Returns the index of the first occurrence of the specified substring from a string, otherwise returns -1. |
lastIndexOf(str: string) | Returns the index of the last occurrence of a value in the string. |
localeCompare(str: string) | Compares the string to the given string in the current locale. |
match(expr: string) | Used to match string to the regular expression. Returns true if there's a match, false otherwise. |
replace(expr: string, replace: string) | Replaces the each occurrence of substring with the new substring. |
search(expr: string) | Searches the string for the value or regular expression and returns the position where it was found. |
slice(start: number, end: number) | Returns a section of a string between the given start/end positions. The second argument is optional. |
split(delim: string) | Splits the string into substrings and returns an array, with each substring identified by the given delimiter |
substr(start: number, length: number) | Returns a string between from the starting position through the given length. |
substring(start: number, end: number) | Returns a section of a string between the given start/end positions. |
toLocaleLowerCase() | Converts string to lower case according to locale format. |
toLocaleUpperCase() | Converts string to upper case according to locale format. |
toLowerCase() | Converts string to lower case. |
toString() | Returns string representation of value. |
toUpperCase() | Converts the string to upper case. |
trim() | Removes all leading and trailing whitespace. |
valueOf() | Returns the string's primitive value of object. |
Number-String Conversion
TypeScript also provides methods that convert numbers to strings. These are particularly helpful when a number requires string operations on is string representation. Table below lists the routines available for converting numbers to strings.
Method | Description |
toExponential() | Returns string representation of exponential number |
toFixed(length: number) | Returns string representation floating point number with the given number of places after the decimal. |
toPrecision(length: number) | Returns string representation of number expressed with the given precision |
toString() | Returns the string representation of number. |
All Chapters