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
4 Answers
Reset to default 2You 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
4 Answers
Reset to default 2You 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
版权声明:本文标题:javascript - Add white space in the beginning and the end of a string - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656164a2161609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论