我感兴趣的是创建一个冷融合UDF,如果字符串中的字符数为1或更少,它将在字符串的开头添加一个不间断的空格。有什么建议吗?
答案 0 :(得分:3)
这是一个允许传递所有参数而不是硬编码的版本。
如果您在某些时候想要的不仅仅是
,或者可能有不同的最小长度,那就很有用。
<cffunction name="prependIfShort" returntype="string" output="false">
<cfargument name="String" type="string" required />
<cfargument name="Prefix" type="string" required />
<cfargument name="Limit" type="numeric" required />
<cfif len(Arguments.String) LTE Arguments.Limit >
<cfreturn Arguments.Prefix & Arguments.String />
<cfelse>
<cfreturn Arguments.String />
</cfif>
</cffunction>
在问题中使用它就像这样:
prependIfShort( Input , ' ' , 1 )
名字可能会有所改善,但这是我现在想到的最好的。
答案 1 :(得分:2)
function prependSpace(myString) {
var returnString=myString;
if (len(myString) LTE 1) {
returnString=" " & myString;
}
return returnString;
}
答案 2 :(得分:2)
添加一些变种:
<cffunction name="padString" returnType="string" access="public" output="no">
<cfargument name="input" type="string" required="yes">
<CFRETURN ((len(ARGUMENTS.input) GT 1) ? ARGUMENTS.input : (" " & ARGUMENTS.input))>
</cffunction>
答案 3 :(得分:1)
// if using cf9+:
function padStr(str){
return len(trim(str)) <= 1 ? 'nbsp;' & str : str
};