Expand env variables in String

梦想的初衷 提交于 2021-01-27 07:33:16

问题


Is there some java utility, which will expand ~ and env. variables in a string?

Like "~/bin;${MY_PATH}" -> "/home/john/bin;/dev/null"

Thank you


回答1:


Basically, you want to do String interpolation with environment variables and expand home directories. I don't know of an easy way to do the latter, but if you use Spring to do your set-up you can use it's PropertyPlaceholderConfigurer to replace placeholders in strings.

By default, environment variables are included in the set of placeholder replacements.

UPDATE: As this is from the user, you can still make use of Spring helper classes:

String stringToBeInterpolated = ....;
Properties properties = System.getProperties();
PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${","}");
String interpolatedValue = helper.replacePlaceholders(stringToBeInterpolated , properties);

This doesn't help with the ~, but at that point I'd assume you can do a fairly simple string replace operation.




回答2:


You can give the full path for the bin folder and use System.getProperty("EVN_PROP_HERE")



来源:https://stackoverflow.com/questions/4593247/expand-env-variables-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!