In TypeScript, the string type is used to represent sequences of characters. Strings can be defined using single quotes ('), double quotes ("), or backticks (`) for template strings.
let message: string = "Hello, TypeScript!";
let greeting: string = 'Welcome!';
let template: string = `This is a TypeScript message`;Template strings
Template strings allow embedding expressions, making it easier to construct complex strings. They are delimited by backticks (`) and can contain interpolated expressions using ${}.
let name: string = "Luis";
let age: number = 30;
let message: string = `Hello, my name is ${name} and I am ${age} years old.`;
console.log(message); // "Hello, my name is Luis and I am 30 years old."Escaping characters
To include special characters in a string, escape sequences are used.
\n: New line\t: Tab\\: Backslash\': Single quote\": Double quote
let multiline: string = "Line 1\nLine 2";
console.log(multiline);
// Line 1
// Line 2String concatenation
String concatenation can be performed using the + operator or the concat method and template strings.
Using the + operator
let greeting: string = "Hello, " + name + ". Welcome to " + text + "!";
console.log(greeting); // "Hello, Luis. Welcome to TypeScript!"Using concat
let concatenatedGreeting: string = greeting.concat(" Enjoy your learning!");
console.log(concatenatedGreeting); // "Hello, Luis. Welcome to TypeScript! Enjoy your learning!"Using template strings
let templateGreeting: string = `Hello, ${name}. Welcome to ${text}!`;
console.log(templateGreeting); // "Hello, Luis. Welcome to TypeScript!"String comparison
Strings can be compared using comparison operators (<, >, <=, >=, ==, !=, ===, !==). Comparisons are case-sensitive.
let string1: string = "abc";
let string2: string = "Abc";
console.log(string1 === string2); // false
console.log(string1.toLowerCase() === string2.toLowerCase()); // trueMethods and properties of string
TypeScript provides a variety of methods and properties for manipulating strings. Let’s take a look at some of the most commonly used.
length
Returns the length of the string.
let text: string = "TypeScript";
console.log(text.length); // 10charAt(index)
Returns the character at the specified position.
console.log(text.charAt(0)); // "T"concat(…strings)
Combines two or more strings.
let part1: string = "Hello, ";
let part2: string = "TypeScript";
console.log(part1.concat(part2)); // "Hello, TypeScript"includes(substring)
Checks if a string contains another string.
console.log(text.includes("Script")); // trueindexOf(substring)
Returns the index of the first occurrence of a substring, or -1 if not found.
console.log(text.indexOf("Script")); // 4replace(substring, newString)
Replaces a substring with another.
console.log(text.replace("Type", "Java")); // "JavaScript"split(separator)
Splits a string into an array of substrings.
let list: string = "apple,banana,pear";
console.log(list.split(",")); // ["apple", "banana", "pear"]substring(start, end)
Returns a substring from the start index to the end index (exclusive).
console.log(text.substring(0, 4)); // "Type"toLowerCase()
Converts all characters in a string to lowercase.
console.log(text.toLowerCase()); // "typescript"toUpperCase()
Converts all characters in a string to uppercase.
console.log(text.toUpperCase()); // "TYPESCRIPT"