在ES6中如何在两个字符串之间提供空格。

huangapple go评论67阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月10日 21:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定