Language: EN

typescript-tipo-string

The String Type in TypeScript

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 = "John";
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 John 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 2

String Concatenation

String concatenation can be done 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, John. Welcome to TypeScript!"

Using concat

let concatenatedGreeting: string = greeting.concat(" Enjoy your learning!");
console.log(concatenatedGreeting);  // "Hello, John. Welcome to TypeScript! Enjoy your learning!"

Using Template Strings

let templateGreeting: string = `Hello, ${name}. Welcome to ${text}!`;
console.log(templateGreeting);  // "Hello, John. 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());  // true

String Methods and Properties

TypeScript provides a variety of methods and properties to manipulate strings. Let’s look at some of the most commonly used.

  • length: Returns the length of the string.
let text: string = "TypeScript";
console.log(text.length);  // 10
  • charAt(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"));  // true
  • indexOf(substring): Returns the index of the first occurrence of a substring, or -1 if not found.
console.log(text.indexOf("Script"));  // 4
  • replace(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 (not inclusive).
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"