Remove all space from the string in typescript

To remove all white space from the string in typescript, use the replace() method with the /\s/g regex it will remove all whitespace from the string.

const str: string = '  first second third  ';

// ✅ Remove all Whitespace
const withoutAnyWhitespace: string = str.replace(/\s/g, '');
console.log(withoutAnyWhitespace); // 👉️ "firstsecondthird"

Remove whitespace from the beginning and end of the string in typescript

To remove space from the start and end of the string in typescript, use the trim() method it will remove space from both sides and return a new string.

const str: string = '  first second third  ';

// ✅ Remove whitespace from beginning and end
const withoutAnyWhitespace: string = str.trim();
console.log(withoutAnyWhitespace); // 👉️ "first second third"

Remove whitespace from the beginning of the string in typescript

To remove space from the left side of the string in typescript, use the trimLeft() method it will delete space from the only left side of the string and return a new string.

const str: string = '  first second third  ';

// ✅ Remove whitespace from beginning of string
const withoutAnyWhitespace: string = str.trimLeft();
console.log(withoutAnyWhitespace); // 👉️ "first second third  "

Remove whitespace from the end of the string in typescript

To remove space from the right side of the string in typescript, use the trimRight() method it will delete space from the only right side of the string and return a new string.

const str: string = '  first second third  ';

// ✅ Remove whitespace from end of string
const withoutAnyWhitespace: string = str.trimRight();
console.log(withoutAnyWhitespace); // 👉️ "  first second third"

I hope it helps you, All the best 👍.