简体   繁体   English

为什么我的文本规范化在不同环境中的行为会有所不同?

[英]Why does my text normalization behave differently in different environments?

I am normalizing some accented text using the following approach / code taken from this answer 我正在使用以下方法/从此答案中获取的代码来规范一些重音文字

Accent removal: 去除口音:

String accented = "árvíztűrő tükörfúrógép";
String normalized = Normalizer.normalize(accented,  Normalizer.Form.NFD);
normalized = normalized.replaceAll("[^\\p{ASCII}]", "");
System.out.println(normalized);

When I run this from with IntelliJ (as part of a unit test), this gives the expected result: 当我使用IntelliJ进行运行时(作为单元测试的一部分),这给出了预期的结果:

arvizturo tukorfurogep

If I run this from the command line (via gradle), I get: 如果从命令行(通过gradle)运行此命令,则会得到:

ArvAztArA tAkArfArAgA

In both cases, I'm using the same PC and Java 1.8.0_151 . 在这两种情况下,我都使用同一台PC和Java 1.8.0_151

The relevant parts from build.gradle : build.gradle的相关部分:

apply plugin: 'java'
apply plugin: 'idea'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
  testCompile group: 'junit', name: 'junit', version: '4.12'
}

What causes this different behaviour? 是什么导致这种不同的行为? And how do I ensure I get the expected result everywhere? 以及如何确保到处都能获得预期的结果?

Thanks to @eckes and others for the compile time suggestion. 感谢@eckes和其他人的编译时间建议。 By specifying an encoding at compile time, I was able to get the desired result. 通过在编译时指定编码,我可以获得所需的结果。

The setting I added to build.gradle was: 我添加到build.gradle的设置是:

compileTestJava.options.encoding = 'UTF-8'

This option only affects the test classes (which is where my issue was). 此选项仅影响测试类(​​这是我的问题所在)。 You can also use: 您还可以使用:

compileJava.options.encoding = 'UTF-8'

if you have text in your production code that needs to be encoded. 如果您的生产代码中有需要编码的文本。

An alternative solution I came across is: 我遇到的另一个解决方案是:

tasks.withType(JavaCompile) {
  options.encoding = 'UTF-8'
}

(Interestingly, none of the above solutions changed the value of the file.encoding system property.) (有趣的是,以上解决方案均未更改file.encoding系统属性的值。)

暂无
暂无

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

相关问题 为什么java.lang.process的readline()在具有相同操作系统的不同框上读取输入流的行为不同 - Why does java.lang.process's readline() behave differently for reading inputstream on different boxes with the same os 为什么我的JButton在不同的计算机上看起来有所不同? - Why does my JButton look differently of different computers? Selenium Webdriver-为什么相同的代码在不同的浏览器驱动程序中表现不同 - Selenium webdriver - why same code behave differently in different browser drivers 为什么proguard -keepclassmembers基于类说明符的行为不同? - Why does proguard -keepclassmembers behave differently based on class-specifier? 为什么String.getBytes()在已编译的jar中表现不同? - Why does String.getBytes() behave differently in a compiled jar? 为什么\ R在Java 8和Java 9之间的正则表达式中表现不同? - Why does \R behave differently in regular expressions between Java 8 and Java 9? 为什么AbstractStringBuilder.append的行为与MIN_VALUE不同? - Why does AbstractStringBuilder.append behave differently for MIN_VALUE? 为什么 Path.relativize 在 Java 8 和 Java 11 上表现不同? - Why does Path.relativize behave differently on Java 8 and Java 11? 为什么 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ 上的“关闭”调用在 C# 和 Java 中表现不同? - Why does the “Close” call on a stream behave differently in C# and in Java? 为什么使用Swing绘制在Java 8和Java 6中的表现不同? - Why does painting with Swing behave differently in Java 8 and Java 6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM