Java的String类的静态方法format可以完成对字符串的格式化,在web应用中替换请求路径中的参数信息时可以使用
这个简单的函数。
使用format比我以前用String的replace来替换参数,高明多了。
简单代码如下:
public class StringTest { public static void main(String[] args) { String requestPath = "http://localhost:8080/mini_web/user=%s&password=%s"; requestPath = String.format(requestPath, "wang","****"); System.out.println("requestPath :"+requestPath); } }
测试结果:
requestPath :http://localhost:8080/mini_web/user=wang&password=****