在我的 android 应用程序中的消息中,如果我给出消息“hi #name,welcome your username:#username and password:#password”并且在消息中#name,#username,#password 将被替换为 iam 从 csv 文件中读取的值和它应该发送消息作为示例:“嗨 praveen,欢迎使用您的用户名:neevarp 和密码:12345”,这些值来自 csv。搜索时我得到了一些链接
Map<String, String> values = new HashMap<String, String>();
values.put("value", x);
values.put("column", y);
StrSubstitutor sub = new StrSubstitutor(values, "%(", ")");
String result = sub.replace("There's an incorrect value '%(value)' in column # %(column)");
但在安卓
StrSubstitutor 类不存在,我认为有没有办法实现这个
这是我从 csv 读取值并通过替换占位符来发送消息的代码
public void sendingSms(String message, String file_path) {
File file = new File("", file_path);
// Read text from file
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int iteration = 0;
while ((line = br.readLine()) != null) {
if (iteration != 0) {
StringBuilder text = new StringBuilder();
text.append(line);
String[] contact = text.toString().split(",");
String phoneNumber = contact[4];
String name = contact[1];
String username = contact[2];
String password = contact[3];
//here i have to replace place holders with name,username,password values
//message.replace("#name", name);
//message.replace("#user", username);
Toast.makeText(Message.this, "" + message,
Toast.LENGTH_SHORT).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null,
null);
}
iteration++;
}
} catch (IOException e) {
e.printStackTrace();
}
}