Introduction

Welcome to this lesson on TypeScript, where we will explore essential string manipulation features including string tokenization, string concatenation, trimming whitespace from strings, and type conversion operations. TypeScript builds upon JavaScript with added type safety and intelligence, providing a robust development environment.

Tokenizing a String in TypeScript

In TypeScript, we can utilize the split method from the String class to tokenize a string. With TypeScript's type system, we can explicitly specify types for more clarity and safety.

let sentence: string = "TypeScript is an amazing language!";
let tokens: string[] = sentence.split(" ");

tokens.forEach((token: string) => console.log(token));

// Output:
// TypeScript
// is
// an
// amazing
// language!

Here, we declare a string variable sentence and specify it should be treated as a string. The tokens variable is typed as a string array string[], providing clear expectations of the variable's contents. The forEach method iterates over each token, ensuring the type remains consistent throughout the operations.

Exploring String Concatenation
Trimming Whitespace from Strings

TypeScript supports the trim method to eliminate extra spaces, with the added benefit of type-checking:

let str: string = "    Hello, World!    "; // string with leading and trailing spaces
str = str.trim(); // remove leading and trailing spaces
console.log(str); // Output: "Hello, World!"
TypeScript Type Conversions

TypeScript enhances type conversion methods by providing compile-time checks. Conversions from strings to numbers and vice versa are made safer:

let numStr: string = "123";
let num: number = parseInt(numStr);
console.log(num);  // Output: 123

let floatStr: string = "3.14";
let pi: number = parseFloat(floatStr);
console.log(pi);  // Output: 3.14

let age: number = 20;
let ageStr: string = age.toString();
console.log(`I am ${ageStr} years old.`); // Output: I am 20 years old.
Integrating String Tokenization and Type Conversions

TypeScript's type safety ensures reliable operations when combining tokenization and type conversions:

let numbers: string = "1,2,3,4,6";
let numArray: string[] = numbers.split(",");
let sum: number = 0;

numArray.forEach((numStr: string) => {
    sum += parseInt(numStr);
});

let average: number = sum / numArray.length;
console.log("The average is " + average);  // Output: The average is 3.2

TypeScript's type declarations and inference help ensure that each component operates as anticipated, minimizing runtime errors.

Summary

Well done! You've mastered key string manipulation techniques in TypeScript, such as string concatenation, string tokenization, trimming whitespace, and type conversions. With TypeScript’s type safety mechanism, these features offer robust and error-resistant coding experiences. Proceed with the exercises to deepen your understanding and enhance your TypeScript skills. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal