admin管理员组文章数量:1313121
Is there a way I can turn this string
let string = 'I have some spaces in it';
into
string = 'iHaveSomeSpacesInIt';
I know I can use
string.split(' ').join('');
to take all the spaces out of the string but how can I transform the first uppercase letter to lowercase and then camelCase at all the spaces that have been removed??
Any help would be appreciated!
Thanks
Is there a way I can turn this string
let string = 'I have some spaces in it';
into
string = 'iHaveSomeSpacesInIt';
I know I can use
string.split(' ').join('');
to take all the spaces out of the string but how can I transform the first uppercase letter to lowercase and then camelCase at all the spaces that have been removed??
Any help would be appreciated!
Thanks
Share Improve this question asked May 4, 2018 at 6:31 Smokey DawsonSmokey Dawson 9,24022 gold badges85 silver badges162 bronze badges 1- 1 split the string-make it as an array-capitalise first character of each array's value-join together to make a camel context string. – Krishna Prashatt Commented May 4, 2018 at 6:34
3 Answers
Reset to default 7Maybe regex can help you lot more faster and produce a more clear code.
var regex = /\s+(\w)?/gi;
var input = 'I have some spaces in it';
var output = input.toLowerCase().replace(regex, function(match, letter) {
return letter.toUpperCase();
});
console.log(output);
Sure, just map
each word (except the first) and capitalize the first letter:
const input = 'I have some spaces in it';
const output = input
.split(' ')
.map((word, i) => {
if (i === 0) return word.toLowerCase();
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
})
.join('');
console.log(output);
Use a specialized library like Lodash for this type of requirement instead of writing a custom logic:
let string = 'I have some spaces in it';
let finalString = _.camelCase(string);
console.log(finalString);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
本文标签: convert string with spaces into string with no spaces and camelCase javascriptStack Overflow
版权声明:本文标题:convert string with spaces into string with no spaces and camelCase javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741932428a2405652.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论