简体   繁体   English

向打字稿中的字符串添加空格(角度)

[英]Adding whitespace to a string in typescript (in angular)

I have a method in one of my components that gets called in my html page whenever I click on a button.我的一个组件中有一个方法,每当我单击一个按钮时,它就会在我的 html 页面中被调用。 I am currently trying to format the page so that certain attributes (namely city and state) appear at fixed locations in the page.我目前正在尝试格式化页面,以便某些属性(即城市和州)出现在页面的固定位置。 This is what I have so far:这是我到目前为止所拥有的:

if (this.city !== undefined) {
   let test = 50;
   test -= this.city.length;
   let result = 'City: ' + this.city;
   for ( let i = 0; i < test; i++) {
       result += ' ';
   }
   return result + this.state;
}

However, instead of seeing something like City:然而,而不是看到像城市这样的东西:

                                       (41 extra spaces) Grapevine TX

it looks like City:它看起来像城市:

Grapevine TX. 

The interesting thing is that extra characters are added as long as they aren't spaces.有趣的是,只要不是空格,就会添加额外的字符。 For example, if I wrote result += 'a', then I would see 41 extra a's in the string.例如,如果我写了 result += 'a',那么我会在字符串中看到 41 个额外的 a。

Using the result += 'a' example, if I wrote the line result = result.replace(/a/g, ' '), then the final output would be City: Grapevine TX.使用 result += 'a' 示例,如果我写了行 result = result.replace(/a/g, ' '),那么最终输出将是 City: Grapevine TX。 However, if I had originally written result += ' ' and then wrote result = result.replace(/ /g, 'a'), then even though the extra spaces would not appear in the original string, those spaces would be replaced by an 'a.'但是,如果我最初写了 result += ' ' 然后写了 result = result.replace(/ /g, 'a'),那么即使额外的空格不会出现在原始字符串中,这些空格也会被替换为一个'a'。

How do I add extra whitespace to my string?如何在我的字符串中添加额外的空格?

Your issue is related to html which omits multiple spaces following another space.您的问题与 html 有关,它在另一个空格之后省略了多个空格。 Angular is not the root cause.角度不是根本原因。

Pure html Example - Both headlines are dispalyed equally :纯 html 示例 - 两个标题均等显示:

 <h1>test test<h1> <h1>test test<h1>

Solution: use tags and/or css for layouting your output.解决方案:使用标签和/或 css 来布局您的输出。

Try use next code in your css:尝试在您的 css 中使用下一个代码:

.pre-class {
    white-space: pre;
}

在此处输入图像描述

But be careful — text will be shown as is with all empty spaces and new rows!但要小心- 文本将按原样显示,所有空格和新行!

而不是''你可以试试&nbsp;

As people have commented you in other answers, the problem in your case is with the html.正如人们在其他答案中对您发表的评论一样,您的问题在于 html。 I recommend you to add &nbsp;我建议你添加&nbsp; instead of ' ' in your code.而不是' '在您的代码中。

if (this.city !== undefined) {
   let test = 50;
   test -= this.city.length;
   let result = 'City: ' + this.city;
   for (let i = 0; i < test; i++) {
       result += '&nbsp;';
   }
   return result + this.state;
}

Or if you prefer, you can embed your string result into <pre></pre> tags.或者,如果您愿意,可以将字符串结果嵌入到<pre></pre>标记中。

You can check out this page https://www.wikihow.com/Insert-Spaces-in-HTML for further information.您可以查看此页面https://www.wikihow.com/Insert-Spaces-in-HTML以获取更多信息。

Even if you get extra spaces in string, html will not render the extra spaces.即使您在字符串中获得额外的空格,html 也不会呈现额外的空格。 You can get the count of whitespaces needed and loop in html wherever wanted.您可以获取所需的空格数并在任何需要的地方循环 html。 Tested and it works.经过测试,它可以工作。

<span *ngFor="let i of whitespacecount">&nbsp;</span>

You can add white spacess by adding \xa0 .您可以通过添加\xa0添加空格。

eg:例如:

result = result.concat("\xa0");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM