Java传递环境变量会自动转义特殊字符

时间:2021-11-16 12:39:33

If i pass some variable as envirinment variable through run configurations in eclipse, then it automatically escapes the special characters. I want to pass some '\n' and '\r' characters for some formatting but these are automatically converted as '\\n' and '\\r' when i check it value in the code by debugging.

如果我通过eclipse中的运行配置将一些变量作为环境变量传递,那么它会自动转义特殊字符。我想传递一些'\ n'和'\ r'字符用于某些格式化,但是当我通过调试在代码中检查它时,它们会自动转换为'\\ n'和'\\ r'。

String value= System.getenv("envPassed");
System.out.println("envPassed= "+value);

So if envPassed is given as 'hi\n\rhello' in Run configurations, then it will be converted as 'hi\\n\\r' and output printed as

因此,如果在运行配置中将envPassed命名为'hi \ n \ rhello',则它将被转换为'hi \\ n \\ r'并输出打印为

hi\n\rhello

whereas the output should come as

而输出应该是

hi
hello

Do we have some way to make it work?

我们有办法让它发挥作用吗?

2 个解决方案

#1


2  

Sequences like \n and \r are only converted to control characters when you enter them where such a behavior is documented, like for String literals in Java source code.

像\ n和\ rr这样的序列只有在输入它们时才转换为控制字符,其中记录了这种行为,例如Java源代码中的字符串文字。

The Eclipse dialogs for environment variables or string substitution do not perform such a conversion, but transfer the string as entered. This is illustrated by the fact that your Java program reading and printing the value of the environment variable prints exactly what you have entered.

环境变量或字符串替换的Eclipse对话框不执行此类转换,而是按输入的方式传输字符串。事实上,您的Java程序读取和打印环境变量的值会准确打印您输入的内容。

It’s a special property of the Java Debugger to show String content like the equivalent Java source code literal upon inspection. This is the only place, where a conversion happens.

它是Java调试器的一个特殊属性,用于在检查时显示类似于Java源代码文字的String内容。这是转换发生的唯一地方。

It doesn’t seem to be the Eclipse developer’s intention to support multi-line strings for these variables. They seem to aim primarily at path strings and similar. However, I found a way, how you can insert a line break. Use ${system_property:line.separator}, this will read the Java system property named line.separator, which is in the list of always present system properties.

它似乎不是Eclipse开发人员打算为这些变量支持多行字符串。他们似乎主要针对路径字符串和类似的。但是,我找到了一种方法,如何插入换行符。使用$ {system_property:line.separator},这将读取名为line.separator的Java系统属性,该属性位于始终存在的系统属性列表中。

#2


-1  

You can always pre-treat the string before manipulating it... if you want to be safe you can use a little bit of regular expressions to ensure this will always work too:

您可以在操作之前始终预处理字符串...如果您想要安全,可以使用一些正则表达式来确保它始终有效:

String value = System.getenv("MyEnv");
if ( value != null ) {
    value.replaceAll("\\\\n(\\\\r)?", "\n" );
}
System.out.println( value );

Please note that the four backslashes are not a mistake - this is necessary because String.replaceAll() interprets the pattern string as both a string and a regular expression.

请注意,四个反斜杠不是错误的 - 这是必要的,因为String.replaceAll()将模式字符串解释为字符串和正则表达式。

Hope this helps!

希望这可以帮助!

#1


2  

Sequences like \n and \r are only converted to control characters when you enter them where such a behavior is documented, like for String literals in Java source code.

像\ n和\ rr这样的序列只有在输入它们时才转换为控制字符,其中记录了这种行为,例如Java源代码中的字符串文字。

The Eclipse dialogs for environment variables or string substitution do not perform such a conversion, but transfer the string as entered. This is illustrated by the fact that your Java program reading and printing the value of the environment variable prints exactly what you have entered.

环境变量或字符串替换的Eclipse对话框不执行此类转换,而是按输入的方式传输字符串。事实上,您的Java程序读取和打印环境变量的值会准确打印您输入的内容。

It’s a special property of the Java Debugger to show String content like the equivalent Java source code literal upon inspection. This is the only place, where a conversion happens.

它是Java调试器的一个特殊属性,用于在检查时显示类似于Java源代码文字的String内容。这是转换发生的唯一地方。

It doesn’t seem to be the Eclipse developer’s intention to support multi-line strings for these variables. They seem to aim primarily at path strings and similar. However, I found a way, how you can insert a line break. Use ${system_property:line.separator}, this will read the Java system property named line.separator, which is in the list of always present system properties.

它似乎不是Eclipse开发人员打算为这些变量支持多行字符串。他们似乎主要针对路径字符串和类似的。但是,我找到了一种方法,如何插入换行符。使用$ {system_property:line.separator},这将读取名为line.separator的Java系统属性,该属性位于始终存在的系统属性列表中。

#2


-1  

You can always pre-treat the string before manipulating it... if you want to be safe you can use a little bit of regular expressions to ensure this will always work too:

您可以在操作之前始终预处理字符串...如果您想要安全,可以使用一些正则表达式来确保它始终有效:

String value = System.getenv("MyEnv");
if ( value != null ) {
    value.replaceAll("\\\\n(\\\\r)?", "\n" );
}
System.out.println( value );

Please note that the four backslashes are not a mistake - this is necessary because String.replaceAll() interprets the pattern string as both a string and a regular expression.

请注意,四个反斜杠不是错误的 - 这是必要的,因为String.replaceAll()将模式字符串解释为字符串和正则表达式。

Hope this helps!

希望这可以帮助!