admin管理员组

文章数量:1026989

I want to add the white space in the beginning and the end of a string if there isn't.

If there is a whitespace at the beginning it will add at the end, and if there is at the end will add at the beginning

var string='This is test';

=>

var string=' This is test ';

or

var string='This is test ';

=> will only add at the beginning

var string=' This is test ';

I want to add the white space in the beginning and the end of a string if there isn't.

If there is a whitespace at the beginning it will add at the end, and if there is at the end will add at the beginning

var string='This is test';

=>

var string=' This is test ';

or

var string='This is test ';

=> will only add at the beginning

var string=' This is test ';
Share Improve this question asked May 7, 2018 at 5:55 EyTaEyTa 1093 silver badges10 bronze badges 2
  • 1 what does you prevent to do so? – Nina Scholz Commented May 7, 2018 at 5:56
  • 3 what have you tied so far ? – Muhammad Usman Commented May 7, 2018 at 5:56
Add a ment  | 

4 Answers 4

Reset to default 2

You can try this code.

    var string='This is test ';
    var result = " "+string.trim()+" "; //trim will remove white space before and after string
    alert(result);

Just try with removing any possible whitespaces and add one on the beginning and end manually:

string = ' ' + string.trim() + ' '

This is simple. Check below:

let string ='This is test';
console.log(string); // No space
console.log(" " + string.trim() + " "); // space at start and end

Use regex and capturing. ^\ |\ $|(^\ \ $) will match beginning space, ending space, and both: use accordingly

I want to add the white space in the beginning and the end of a string if there isn't.

If there is a whitespace at the beginning it will add at the end, and if there is at the end will add at the beginning

var string='This is test';

=>

var string=' This is test ';

or

var string='This is test ';

=> will only add at the beginning

var string=' This is test ';

I want to add the white space in the beginning and the end of a string if there isn't.

If there is a whitespace at the beginning it will add at the end, and if there is at the end will add at the beginning

var string='This is test';

=>

var string=' This is test ';

or

var string='This is test ';

=> will only add at the beginning

var string=' This is test ';
Share Improve this question asked May 7, 2018 at 5:55 EyTaEyTa 1093 silver badges10 bronze badges 2
  • 1 what does you prevent to do so? – Nina Scholz Commented May 7, 2018 at 5:56
  • 3 what have you tied so far ? – Muhammad Usman Commented May 7, 2018 at 5:56
Add a ment  | 

4 Answers 4

Reset to default 2

You can try this code.

    var string='This is test ';
    var result = " "+string.trim()+" "; //trim will remove white space before and after string
    alert(result);

Just try with removing any possible whitespaces and add one on the beginning and end manually:

string = ' ' + string.trim() + ' '

This is simple. Check below:

let string ='This is test';
console.log(string); // No space
console.log(" " + string.trim() + " "); // space at start and end

Use regex and capturing. ^\ |\ $|(^\ \ $) will match beginning space, ending space, and both: use accordingly

本文标签: javascriptAdd white space in the beginning and the end of a stringStack Overflow