問題描述
我正在嘗試在 ColdFusion 中規范化字符串.
I'm trying to normalize a string in ColdFusion.
我想為此使用Java類java.text.Normalizer
,因為據我所知CF沒有任何類似的功能.
I want to use the Java class java.text.Normalizer
for this, as CF doesn't have any similar functions as far as I know.
這是我當前的代碼:
<cfset normalizer = createObject( "java", "java.text.Normalizer" ) />
<cfset string = "?é?è" />
<cfset string = normalizer.normalize(string, createObject( "java", "java.text.Normalizer$Form" ).NFD) />
<cfset string = ReReplace(string, "\p{InCombiningDiacriticalMarks}+", "") />
<cfoutput>#string#</cfoutput>
任何想法為什么它總是輸出 ?é?è
而不是規范化字符串?
Any ideas why it always outputs ?é?è
and not a normalized string?
推薦答案
在 ColdFusion 中,與 Java 不同,您不需要在字符串文字中轉義反斜杠.您當前的正則表達式不會匹配不以反斜杠開頭的任何內容,因此不會發生替換.
In ColdFusion, unlike in Java, you don't need to escape backslashes in string literals. Your current regex will not match anything that does not start with a backslash, so no replacement happens.
除此之外,您的代碼完全正確,您可以看到輸出時字符串的長度是 8,而不是 4.這是 normalize
調用的效果.
Other than that, your code is perfectly correct and you can see that the length of the string is 8, not 4, at the time of the output. This is an effect of the normalize
call.
但是,請記住,它仍然是原始字符串的等效表示,因此您無法從視覺上區分差異也就不足為奇了.這是正確的 Unicode 渲染.
However, remember that it is still an equivalent representation of the original string, and so it is not surprising that you cannot tell the difference visually. This is correct Unicode rendering in action.
這篇關于在 ColdFusion 中規范化字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!