Remove space in string

Java Script doesn’t provide trim() function to remove space of string.

Thus please use replace() function to do it.

Source Code

function myFunction(){
  var str = '        What is your name?         ';
  Browser.msgBox(str.replace(/\s/g, ''));
}

Format

str.replace(val1, val2)

Parameters

(1) str

Original string

(2) val1

Targeted string to be replaced

(3) val2

New string to be replaced from val1

Return

string

Custom Function

This customized function is almost same with trim() function.

function customTrim(str){
  return str.replace(/\s/g, '');
}

Leave a Comment