在字符串中扩展环境变量 [英] Expand env variables in String

查看:70
本文介绍了在字符串中扩展环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些Java实用程序,它将扩展〜和env.字符串中的变量?

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

就像〜/bin; $ {MY_PATH}"->"/home/john/bin;/dev/null"

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

谢谢

推荐答案

基本上,您希望使用环境变量进行字符串插值并扩展主目录.我不知道要实现后者的简单方法,但是如果您使用Spring进行设置,则可以使用

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.

更新:由于这是来自用户的,因此您仍然可以使用Spring帮助器类:

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.

这篇关于在字符串中扩展环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆