在Coldfusion中为字符串添加空格。

问题描述 投票:0回答:1

我需要在一个字符串的末尾添加空白字符。

ColdFusion 似乎会自动删除我尝试添加的任何空白字符,我有时会得到一个空的空格字符,但就是这样。

我有一个输入字段,一个字符串,可以长达7个字符。如果输入字段少于7个字符,我就要在末尾加上空格。

有谁知道有什么快速、简单、直观的方法可以用 coldfusion 来实现这个功能。尽可能少的代码为佳。

我试过的一些解决方案是

 #LEFT(FORM.strInput & '       ', 7)#

 #LEFT(FORM.strInput & '        0', 7)#

 #REPLACE(LEFT(FORM.strInput & 0000000, 7), '0', ' ', 'all')#

<CFLOOP FROM="1" TO="7 - LEN(FORM.strInput)">
  <CFSET FORM.strInput = FORM.strInput & ' '>
</CFLOOP>

<CFLOOP FROM="1" TO="7 - LEN(FORM.strInput)">
  <CFSET FORM.strInput = FORM.strInput & '&nbsp;'>
</CFLOOP>
string coldfusion append concatenation whitespace
1个回答
5
投票

你可以使用 repeatString()

<cfset form.strInput = form.strInput & repeatString( ' ', max( 7-len(form.strInput), 0 ) ) />

但是,请记住,如果你试图在一个网页上显示多个连续的空格,浏览器只会 "显示 "第一个。


2
投票

我喜欢Scott的答案,但我会对它进行一些改进。

<script>
form.strInput &= repeatString(' ', max(7 - len(form.strInput), 0));
</script>
© www.soinside.com 2019 - 2024. All rights reserved.