英文:
How to provide space between two strings in es6
问题
I want to provide space between name and age, so I have used \u00A0\u00A0\u00A0
. It's working fine, but it doesn't look good. Is there another way to implement this?
personsList: ${t('Name')} ${personName}\u00A0\u00A0\u00A0${t('age')} ${personAge}
英文:
I want to provide space between name and age , so i have used \u00A0\u00A0\u00A0
its working fine, But it not looks good is there another way to implement this
personsList: `${t('Name')} ${personName}\u00A0\u00A0\u00A0${t('age')} ${personAge}
答案1
得分: 1
我猜你要在某个时候将personsList
呈现为HTML,并且希望在名字和随后的年龄之间有多个空格,这就是为什么你使用非断开空格而不仅仅是正常的空格的原因。
如果这是真的,一种方法是稍微重构一下,将名字和年龄信息放在不同的元素中,具有适当的填充等。
如果你确实必须通过文本来做,你现在的做法是可行的。你说它看起来不好。你可以通过使用非断开空格的常数,也许使用repeat
来使它更明确:
// ...
personsList: `${t('Name')} ${personName}${nbsp.repeat(3)}${t('age')} ${personAge}
或类似的,但使用一个定义好的间隔。
// ...
personsList: `${t('Name')} ${personName}${spacer}${t('age')} ${personAge}
但我建议像上面描述的那样进行重构。
英文:
I'm going to guess at some point you're rendering personsList
to HTML and you want more than just one space in between the name and the following age, which is why you're using non-breaking spaces rather than just normal spaces.
One approach if that's true is to refactor slightly and put the name and age information in different elements with appropriate padding and such.
If you absolutely have to do it via text, what you have is workable. You've said it doesn't look good. You could make it more explicit by using a constant for the non-breaking space, and perhaps using repeat
:
const nbsp = "\u00A0";
// ...
personsList: `${t('Name')} ${personName}${nbsp.repeat(3)}${t('age')} ${personAge}
Or similar, but with a defined spacer.
const spacer = "\u00A0\u00A0\u00A0";
// ...
personsList: `${t('Name')} ${personName}${spacer}${t('age')} ${personAge}
But I'd recommend refactoring as described above instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论