0

I have a String:

String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p>&nbsp;</p><p><span>Congrats!.....</span></p>";

So I want to take CustomerName and accountName words and replace with customers details. Can anyone please tell me how can I replace. Here customerName and accountName are dynamically changing ..because those are columns in database sometimes different columns. So i want to find the words within the { and } and need to replace with column data.

5 Answers5

1

Use the following code

s = s.replace("{customerName}", realCustomerName);
s = s.replace("{accountName}", realAccountNAme);

With String's replace function, the first argument is the string you want to replace, and the second argument is the string you want to insert.

DonyorM
  • 1,760
  • 1
  • 20
  • 34
0

Try:

s=s.replace('{customerName}',CustomerName ).replace('{accountName}',accountName);

where CustomerName and accountName will be the strings holding your customers details

laaposto
  • 11,835
  • 15
  • 54
  • 71
  • Also really hard to read. I would suggest putting code like this on multiple lines. – DonyorM Apr 23 '14 at 10:26
  • actually i need to replace details while sending mail to the customers. Those are not fixed those are database columns and it may be changed dynamically.that is mail template need to send – Rajasekhar Burepalli Apr 23 '14 at 11:03
  • So every time you get the values dynamically change and the value of the string with this code – laaposto Apr 23 '14 at 11:05
0

If you simply want to replace the words, you could do the following:

String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p>&nbsp;</p><p><span>Congrats!.....</span></p>";
s.replace( "{customerName}", customer.getName() );
s.replace( "{accountName}", account.getName() );

Or, if you are building the string yourself and you can modify it, it might be better to do the following:

String s="<p>Dear <span>%1$s, your %1$s is actived </span></p><p>&nbsp;</p><p><span>Congrats!.....</span></p>";
// You may also just create a new String object...
s = String.format( s, customer.getName(), account.getName() );
Smokez
  • 382
  • 1
  • 5
0

Finally, I found the answer to replace the words using regular expressions. Here words b/w ~ need to replace and these words are not fixed and dynamically will be added to string from UI text Area.

import java.util.regex.Matcher; import java.util.regex.Pattern;

public class RegularEx {

/**
 * @param args
 */

public static void main(String args[]) {
    Pattern pattern = Pattern.compile("\\~.*?\\~");
    StringBuilder s = new StringBuilder(
            "~ABCD~~BBCc~All the best ~ABCD~~BBCc~~in~~Raja~ Such kind of people ~in~~Raja~~ABCD~~BBCc~~in~~Raja~rajasekhar~ABCD~~BBCc~~in~~Raja~ Bayanapalli ~Chinthalacheruvu~");
    Matcher matcher = pattern.matcher(s);
    // using Matcher find(), group(), start() and end() methods
    String s1 =new String("~ABCD~~BBCc~All the best ~ABCD~~BBCc~~in~~Raja~ Such kind of people ~in~~Raja~~ABCD~~BBCc~~in~~Raja~rajasekhar~ABCD~~BBCc~~in~~Raja~ Bayanapalli ~Chinthalacheruvu~");
    int i = 0;
    while (matcher.find()) {
        String grp = matcher.group();
        int si = matcher.start();
        int ei = matcher.end();
        System.out.println("Found the text \"" + grp

        + "\" starting at " + si + " index and ending at index " + ei);
        s1=s1.replaceAll(grp, "Raja");
        //System.out.println("FinalString" + s1);
    }
    System.out.println("------------------------------------\nFinalString" + s1);

}

}

-1
 s = s.replace("{customerName}", "John Doe");
 s = s.replace("{accountName}", "jdoe");
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Biber
  • 709
  • 6
  • 19