簡體   English   中英

如何在 java 中使用 MessageFormat 格式化郵件內容

[英]How can i format the content of the mail by using MessageFormat in java

我有一個想要在 java 中發送的自動郵件內容。 我想使用 MessageFormat 將其格式化為 java。

這是包含三個要自定義的參數的郵件內容。

Bonjour,

Nous vous confirmons la reception du {0}$ correspondant à l'achat de votre
{1} correspondant au forunisseur {3}

Si cela vous convient, nous vous enverrons la facture detaille avec toute les justificatifs
et le detail des commandes

Nous restons à votre entière disposition pour toute informations complementaires

A très bientôt.

Ceci est un message automatique , merci de ne pas repondre à ce mail.

這些參數將在一個數組中檢索並插入到郵件的內容中

String[] data = new String[] {"15","p1","Tera"};
String montant = data[0];
String produit = data[1];
String fournisseur = data[2];
String message = "Bonjour, ..."; //The content of the mail above
MessageFormat mf = new MessageFormat(message);
System.out.println(mf);

我想將消息顯示為郵件內容,以及如何傳遞我的三個字符串變量而不是 {0}、{1} 和 {2}。 如何在 java 中做到這一點?

你可以做:

String message = "Bonjour, ..."
MessageFormat mf = new MessageFormat(message); 
String formattedStr = mf.format(new Object[]{"15", "p1", "Tera"});

注意- 單引號'應該通過加倍單引號來轉義: ''

未轉義的報價:

String msg = "Bonjour,\n" +
        "{0}$ correspondant à l'achat de votre {1} correspondant au forunisseur {2}\n";
MessageFormat mf = new MessageFormat(msg);
String formattedStr = mf.format(new Object[]{"15", "p1", "Tera"});
System.out.println(formattedStr);

不正確的 output:

Bonjour,
15$ correspondant à lachat de votre {1} correspondant au forunisseur {2}

不是我們想要的...

要修復它,我們應該轉義引用l'achat --> l''achat

String msg = "Bonjour,\n" +
        "{0}$ correspondant à l''achat de votre {1} correspondant au forunisseur {2}\n";
MessageFormat mf = new MessageFormat(msg);
String formattedStr = mf.format(new Object[]{"15", "p1", "Tera"});
System.out.println(formattedStr);

正確的 output:

Bonjour,
15$ correspondant à l'achat de votre p1 correspondant au forunisseur Tera

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM