今天小编简单记录下如何使用java提供的API来对字符串消息进行替换或者匹配操做,可是,这并不是正则表达式。java
平常开发中,咱们常常将一些配置信息记录到诸如.properties
文件当中,举个小栗子,当咱们在开发微信公众号时,咱们就会想着把某一个微信api接口路径url配置到wechat.properties
中,下面咱们就假设该url为:正则表达式
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
api
这是微信获取access_token
的接口路径,很明显,它须要咱们传入4个参数,分别是appid
、secret
、code
、grant_type
,因为参数值是可变的,因此一般咱们不会像下面这样直接将整个url地址配置到wechat.properties
文件当中,错误的示范以下:bash
wechat.properties
微信
wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
复制代码
若是咱们在程序中提取wechatOAuthTokenUrl
变量的值,那么对参数的赋值操做将变成一件很是棘手的事情。一般的作法是咱们只配置url地址的前一小部分,即至关长时间不会改变的部分,即:app
wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token
async
而后再程序中,当咱们想要发送请求的时候,只须要将4个参数构形成一个map便可发起请求,示例以下:学习
public static void main(String[] args) throws IOException {
// 读取属性文件,通常不会这么作,集成Spring时可作配置
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
properties.load(inputStream);
String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
// 参数map
Map<String, String> map = new HashMap<>();
map.put("appid", "A");
map.put("secret", "B");
map.put("code", "C");
map.put("grant_type", "D");
// 请求参数,封装了url、参数、header等信息
RequestParam param = new RequestParam();
param.setUrl(wechatOAuthTokenUrl);
param.setBody(map);
// 发送请求
HttpClientUtil.getInstance().asyncSend(param);
}
复制代码
那么还有没有其余方式呢?额,有,下面的这种方式采用占位符来完成参数的匹配,占位符匹配不是什么高大上的东西,原则上采用正则表达式也能够完成,可是没有必要采用正则表达式。测试
java.text
包下的MessageFormat
类采用该类,咱们只须要将属性配置成以下格式便可: wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}
url
测试类以下:
public class PropertiesFormater {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
properties.load(inputStream);
// 先打印出未替换的wechatOAuthTokenUrl属性值
String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
System.err.println(wechatOAuthTokenUrl);
// 使用MessageFormat对字符串进行参数替换
wechatOAuthTokenUrl = MessageFormat.format(wechatOAuthTokenUrl,
new Object[] { "PPID", "SECRET", "CODE", "authorization_code" });
System.err.println(wechatOAuthTokenUrl);
}
}
复制代码
输出以下:
https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}
https://api.weixin.qq.com/sns/oauth2/access_token?appid=PPID&secret=SECRET&code=CODE&grant_type=authorization_code
复制代码
String.format()
进行匹配url属性格式配置以下: wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=%s
测试类以下:
public class PropertiesFormater {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
properties.load(inputStream);
// 先打印出未替换的wechatOAuthTokenUrl属性值
String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
System.err.println(wechatOAuthTokenUrl);
wechatOAuthTokenUrl = String.format(wechatOAuthTokenUrl, new Object[] { "A", "B", "C", "D" });
System.err.println(wechatOAuthTokenUrl);
}
}
复制代码
输出以下:
https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=%s
https://api.weixin.qq.com/sns/oauth2/access_token?appid=A&secret=B&code=C&grant_type=D
复制代码
能够看出第一种方式采用大括号进行匹配,第二种方式采用像C语言那样的%s
匹配,并且,本文只是简单演示了匹配的一种状况,足以解决咱们的需求,String的占位符匹配还有不少,你们请各自深刻学习之,谢谢阅读!