2013-10-14 36 views
1

在我的Android应用程序 在消息中,如果我给消息“嗨#名称,欢迎您的用户名:#用户名和密码:#密码”和消息#名称,#用户名,#密码将被替换其值从IAM csv文件 读,它应该发送消息的例子:“喜的Praveen,欢迎您的用户名:neevarp和密码:12345” 和这些价值观是从CSV。而搜索我得到了一些链接在Android中的命名占位符

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)"); 

但在android

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(); 
    } 
} 

回答

0

函数如果你想设计你自己的StrSubstitutor类,你想要的一切就建立在String类本身中。基本上用您的Mapped值构建/设计一个foreach到函数中。

String result = inputString.replace(valueString, replacedValueString); 

但我不知道你要求内置的功能。 Alex Fu也提供了替代方法,您可以通过它来处理您的字符串替换。