1

In my android app in message if i give message "hi #name,welcome your username:#username and password:#password" and in message #name,#username,#password are to be replaced with values iam reading from csv file and it should send message as example:"hi praveen,welcome your username:neevarp and password:12345" and those values are from csv .while searching i got some link

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

but in android

StrSubstitutor class is not there i think so is there any way to implement this

here is my code of reading values from csv and sending messages by replacing place holders

      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();
    }
}
Community
  • 1
  • 1
praveen
  • 420
  • 4
  • 18

2 Answers2

1

You should really be using the built in string formatting Android provides via string resources: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Alex Fu
  • 5,509
  • 3
  • 31
  • 40
  • hey Alex Fu ,i have added my code i am not sure how it is useful, can you please explain me how it will be useful in my case – praveen Oct 14 '13 at 13:22
0

The functionality that you want is built right into the String class itself if you want to design your own StrSubstitutor class. Essentially building/designing a foreach with your Mapped values into the function.

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

But I am unaware of the function that you are requesting being built-in. Alex Fu as well has provided alternate means by which you could handle your string replacement.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53