gpt4 book ai didi

android - Android 中的命名占位符

转载 作者:行者123 更新时间:2023-11-30 03:19:50 27 4
gpt4 key购买 nike

在我的安卓应用中在消息中,如果我给出消息“你好#name,欢迎你的用户名:#username 和密码:#password”,在消息中#name,#username,#password 将被替换为我从 csv 文件读取的值它应该发送消息,例如:“嗨 praveen,欢迎您的用户名:neevarp 和密码:12345”这些值来自 csv。在搜索时我得到了一些链接

Named placeholders in string formatting

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 class is not there i think so is there any way to implement this

这是我从 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();
}
}

最佳答案

您真的应该使用 Android 通过字符串资源提供的内置字符串格式:http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

关于android - Android 中的命名占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19360433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com