String Operators in TypeScript with Examples

What is String Operators in TypeScript?

String operators in TypeScript are used to manipulate and work with string values.

List of String Operators in TypeScript

The following are the list of string operators with Examples.

String Concatenation (+) Operator

The (+) operator is used to combine (concatenate) two or more strings.

Here’s an example:

let greeting = "Hi, welcome to";
let website = "Itsourcecode";
let message = greeting + ", " + website + "!";
console.log(message); 

Output:

Hi, welcome to, Itsourcecode!

Assignment (+=) Operator

This operator is used to add a string to the end of an existing string.

Here’s an example:

let message = "Hi, welcome to";
message += ", Itsourcecode!";
console.log(message);

Output:

Hi, welcome to, Itsourcecode!

Template Literals (`)

The Template literals allow the inclusion of expressions within strings using backticks (`).

This enables multi-line strings and string interpolation.

Here’s an example:

let uName = "Code";
let age = 18;
let greeting = `Hi, my name is ${uName} and I am ${age} years old.`;
console.log(greeting);

Output:

Hi, my name is Code and I am 18 years old.

String Interpolation

The String interpolation is similar to template literals, allowing variables to be inserted into strings.

Here’s an example:

let item = "iPhone";
let price = 90000; 
let result = `The price of the ${item} is ${price}.`;
console.log(result);

Output:

The price of the iPhone is 90000.

String Methods

The String methods consist of various methods that is used for manipulating strings.

Here’s an example:

let SampleText = "Hi, Welcome to Itsourcecode!";
let upperText = SampleText.toUpperCase();
let lowerText = SampleText.toLowerCase();
let trimmedText = SampleText.trim();
console.log(upperText); 
console.log(lowerText); 
console.log(trimmedText);

Output:

HI, WELCOME TO ITSOURCECODE!
hi, welcome to itsourcecode!
Hi, Welcome to Itsourcecode!

String Length Property (length)

The String Length Property returns the length of a string.

Here’s an example:

let SampleText = "Hi, Welcome to Itsourcecode!";
let result = SampleText.length;
console.log(result);

Output:

28

Conclusion

The String operators in TypeScript are essential tools for manipulating and working with string values.

Key operators include:

  • String Concatenation (+): Combines two or more strings.
  • Assignment (+=): Adds a string to the end of an existing string.
  • Template Literals (`): Allows multi-line strings and string interpolation.
  • String Interpolation: Inserts variables into strings.
  • String Methods: Various methods for manipulating strings, such as toUpperCase(), toLowerCase(), and trim().
  • String Length Property (length): Returns the length of a string.

These operators and methods provide powerful ways to handle and manipulate Strings in TypeScript.

I hope this article has provided you with insights and helped you understand the String operators in TypeScript.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment