我有一个短信模板,如下所示
用户:XXXXXXXXXXXXXXXXXX,电话:XXXXXXXXXX,Loc:XXXXXXXXXXXXXXXXXXXX,区域:XXXXXXXXXXXXXXXXXXXX,为您添加了送货上门服务。请查看详情
其中XXXXXX需要填充动态值
我试过这种方式,如下所示
但是当我运行程序时,我得到的输出是
User: Kiran, Ph: 11111111111111, Loc: KiranXX, Area: KiranXX, added you for Home Delivery. Please review details
最后添加XX 这是我的程序
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
public class Test {
final static Logger logger = Logger.getLogger(Test.class);
public static void main(String args[]) throws UnknownHostException
{
String user = "Kiran";
String phone = "11111111111111";
String loc = "my location";
String area = "my area";
String message = "User: XXXXXXXXXXXXXXXXXX, Ph: XXXXXXXXXX, Loc: XXXXXXXXXXXXXXXXXXXX, Area: XXXXXXXXXXXXXXXXXXXX, added you for Home Delivery. Please review details\r\n" +
"";
message = message.replace("XXXXXXXXXXXXXXXXXX", user);
message = message.replace("XXXXXXXXXX", phone);
message = message.replace("XXXXXXXXXXXXXXXXXXXX", loc);
message = message.replace("XXXXXXXXXXXXXXXXXXXX", area);
System.out.println(message);
}
}
你能告诉我如何解决这个问题吗?
答案 0 :(得分:5)
你应该用这个:
String template = "User: %s, Ph: %s, Loc: %s, Area: %s, added you for Home Delivery. Please review details\r\n";
String result = String.format(template, user, phone, loc, area);
答案 1 :(得分:1)
首先替换肯定是错误的。
我会像这样编码:
修改强>
message = message.replace("User: XXXXXXXXXXXXXXXXXX", "User: " +user);
答案 2 :(得分:0)
我的建议是:
message = message.replace( "User:\\s+X+", "User: " + user );
这将: