0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

replaceトラップ

Last updated at Posted at 2019-10-21

##だまされたわ

##メールで試してみる

MailTemplate.java
package mail;

public class MailTemplate {

	/** from */
	private String from;
	/** to */
	private String to;
	/** cc */
	private String cc;
	/** title */
	private String title;
	/** body */
	private String body;

	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	public String getCc() {
		return cc;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
}

本文を作成するならこんな感じですよね

Test1.java
package test;

import mail.MailTemplate;

/**
 * test
 *
 * @author me
 *
 */
public class Test1 {

	/** mail template */
	private static MailTemplate mail = new MailTemplate();

	/** body template */
	private static final String BODY = "[time]に[place]でまちあわせな。\r\nこなかったら[kill]。";

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(join(createMail()));
	}

	private static String join(MailTemplate mail) {
		String crlf = "\r\n";
		return mail.getFrom() + crlf + mail.getTo() + crlf + mail.getCc() + crlf + mail.getTitle() + crlf + mail.getBody();
	}

	/**
	 * create
	 * @return
	 */
	private static MailTemplate createMail() {

		// from
		mail.setFrom("おくりぬし");
		// to
		mail.setTo("おくりさき");
		// cc
		mail.setCc("ほかのひと");
		// title
		mail.setTitle("けんめい");
		// body
		mail.setBody(replaceBody(BODY));

		return mail;
	}

	/**
	 * replace
	 * @param text
	 * @return
	 */
	private static String replaceBody(String text) {
		text.replace("[time]", "2019/12/25");
		text.replace("[place]", "東京駅");
		text.replace("[kill]", "びんた");
		return text;
	}

}

で、結果

おくりぬし
おくりさき
ほかのひと
けんめい
[time]に[place]でまちあわせな。
こなかったら[kill]。

##replaceって
Getter Setterみたいにできないわけですね

ただしくは

Test2.java
package test;

import mail.MailTemplate;

/**
 * test
 *
 * @author me
 *
 */
public class Test2 {

	/** mail template */
	private static MailTemplate mail = new MailTemplate();

	/** body template */
	private static final String BODY = "[time]に[place]でまちあわせな。\r\nこなかったら[kill]。";

	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(join(createMail()));
	}

	private static String join(MailTemplate mail) {
		String crlf = "\r\n";
		return mail.getFrom() + crlf + mail.getTo() + crlf + mail.getCc() + crlf + mail.getTitle() + crlf + mail.getBody();
	}

	/**
	 * create
	 * @return
	 */
	private static MailTemplate createMail() {

		// from
		mail.setFrom("おくりぬし");
		// to
		mail.setTo("おくりさき");
		// cc
		mail.setCc("ほかのひと");
		// title
		mail.setTitle("けんめい");
		// body
		mail.setBody(replaceBody(BODY));

		return mail;
	}

	/**
	 * replace
	 * @param text
	 * @return
	 */
	private static String replaceBody(String text) {
		text = text.replace("[time]", "2019/12/25")
				.replace("[place]", "東京駅")
				.replace("[kill]", "びんた");
		return text;
	}

}

結果

おくりぬし
おくりさき
ほかのひと
けんめい
2019/12/25に東京駅でまちあわせな。
こなかったらびんた。

しっかり上書きしないとなんも変わらないの、以外と盲点でキレそう

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?