Strings in TypeScript are represented by the String data type.String data type in typescript is similar to strings in other languages.string values are enclosed in single or double quotes.For example to declare a string variable we surround string values in single or double quotes
name: string = "Ajay";
or
name:string='Ajay';
One nice feature of strings in typescript is that we can declare template string.A template string is a string which is enclosed in backquote (`) character.
The advantage of using template string is that it can expand multiple lines and may also contain embedded expressions .Embedded expressions are declared as ${ expr } and are used to represent a variable.
String concatenation
We can concatenate or join two strings using the + character.
firstName: string = "Ajay"; lastName: string = "Kumar"; fullName:string; fullName=firstName+lastName;//full name will contain the value Ajay Kumar
we can also use the concat() method of string type to concatenate 2 string.
Some useful properties of string type are:
String member |
Use |
Length | to get the length of the string |
concat() | to concatenate 2 strings |
indexOf() | to get the occurence and starting index of a substring.It returns -1 if substring is not contained in the main string. |
substr() | returns a substring |
Leave a Reply