作为环境变量传递时,无法解码货币符号的unicode

时间:2018-08-21 18:05:19

标签: java unicode

以下代码适用于卢比符号。

String encode ="\u20B9";
byte[] ptext = encode.getBytes(StandardCharsets.UTF_8);
String value = new String(ptext,StandardCharsets.UTF_8);
System.out.print(value);

It prints : ₹

但是当我们将Rupee Symbol Unicode作为环境变量传递时,它就无法工作

String encode = System.getenv("encode");
byte[] ptext = encode.getBytes(StandardCharsets.UTF_8);
String value = new String(ptext,StandardCharsets.UTF_8);
System.out.print(value);

It prints : \u20B9

这是我在Mac OS上尝试过的。

2 个答案:

答案 0 :(得分:3)

Java仅在Java源文件中包含\uXXXX格式,并且编译器对其进行处理(解析并将其转换为单个字符)时,才能理解。如果您从某个地方读取该值,它将不会被解释为Unicode字符(.properties文件中的文件除外,这有点特殊)。

答案 1 :(得分:0)

复制粘贴字符

粘贴实际的字符,而不是像Kayaman在correct Answer中所指出的那样使用\u转义符。

macOS High Sierra 2018.2.2内与Java 10.0.1Zulu Azul SystemsIntelliJ)一起在enter image description here上工作。

UnicodeChecker

package com.basilbourque.example;

public class Env {
    // Example passing Unicode characters via system environment variable.
    public static void main ( String[] args ) {
        System.out.println( "Bonjour tout le monde." );
        System.out.println( System.getProperty( "java.version" ) );
        System.out.println( System.getenv( "wazzup" ) );
        System.out.println( System.getenv( "fire" ) );
        System.out.println( System.getenv( "rupee" ) );
    }
}
  

Bonjour tomont le monde。

     

10.0.1

     

午餐

     

     

UnicodeChecker应用

提示:您可以在出色的免费macOS应用{{3}}中找到并复制所有Unicode字符。

相关问题