在ColdFusion中规范化字符串 [英] Normalize String in ColdFusion
问题描述
我正在尝试在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中规范化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!