0

I am trying to replace parts of String starting with $ (I manually use parameters in String) but for some reason it fails. Here's what I have:

 public void sendNotificationOfAcceptedMeeting(String candidateName, DateTime dateTime) {
 Map<String,String> params = new HashMap<String,String>();
 params.put("date", dateTime.toString(DATETIME_FORMAT));
 params.put("candidate", candidateName);
 sendEmailWithTemplate(TEMPLATE_ALL_ACCEPTED, params);
 }

then:

private void sendEmailWithTemplate(String templateName, Map<String, String> params) {
 EmailTemplate template = emailTemplateDao.getEmailTemplate(templateName);
 String subject = applyTemplateParameters(template.getSubject(), params);
 String body = applyTemplateParameters(template.getTetxBody(), params);
 sendEmail(subject, body);
}

and finally:

private String applyTemplateParameters(String templateText, Map<String, String> params) {
 String toReturn = templateText;
 if (params != null) {
 System.out.println("map size: " + params.size());
 for (Entry<String, String> entry : params.entrySet()) {
 System.out.println("********************");
 System.out.println(toReturn + " ***BEFORE");
 toReturn.replace("$" + entry.getKey(), entry.getValue());
 System.out.println("$" + entry.getKey() + " ***KEY");
 System.out.println(entry.getValue() + " ***VALUE");
 System.out.println(toReturn + " AFTER");
 }
 }
 return toReturn;
}

As you probably figured, the second method calls the third method with String that I want to modify and map of parameters and their values. Second method sets the vlue of template String to:

 The meeting request at $date with $candidate was accepted by all interviewers.

However the sysouts in third method print:

 map size: 2
********************
The meeting request at $date with $candidate was 
accepted by all interviewers. ***BEFORE
$candidate ***KEY
Acceeedsasd Stetfghfghasd ***VALUE
The meeting request at $date with $candidate was 
accepted by all interviewers. AFTER
********************
The meeting request at $date with $candidate was 
accepted by all interviewers. ***BEFORE
$date ***KEY
27/12/2014 8:00 ***VALUE
The meeting request at $date with $candidate was 
accepted by all interviewers. AFTER

which clearly shows that it doesn't replace parameters... I thought maybe $ breaks it (regex) but replace(), unlike replaceAll() takes String - not regex. I'm out of ideas, any help appreciated.

asked Dec 22, 2014 at 11:11

4 Answers 4

3

You have to do this:

 toReturn = toReturn.replace("$" + entry.getKey(), entry.getValue());

and then print the value of toReturn

answered Dec 22, 2014 at 11:15
Sign up to request clarification or add additional context in comments.

1 Comment

:).. Atleast you got the answer :)
1

Try replacing the "$" in your code with "0024円"

Like this:

toReturn = toReturn.replace("0024円" + entry.getKey(), entry.getValue());

answered Dec 22, 2014 at 11:17

1 Comment

As I said, $ was not a problem since replace takes String as input, not regex. Thanks for trying though :)
1

The problem is that the replace method returns a String and this wasn't being used. See below where I have put the result into replacedText.

String replacedText = toReturn.replace("$" + entry.getKey(), entry.getValue());
System.out.println("$" + entry.getKey() + " ***KEY");
System.out.println(entry.getValue() + " ***VALUE");
System.out.println(replacedText+ " AFTER");
answered Dec 22, 2014 at 11:17

Comments

1

String is immutable, cant be replaced, just

toReturn = toReturn.replace("$...","")
answered Dec 22, 2014 at 11:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.