简体   繁体   English

如何在Java中的某个单词之后附加单词

[英]How to append a Word after a certain word in java

I have got this key value pairs under my properties file 我的属性文件中有此键值对

mailsubject = REDEX Application --- Credentials
mailtext = Dear User,\n \
          Following Are the credentials for REDEX Mapping Application  -- pwd
setFrom = xxxs31@gmail.com

This is where i am using them in my java file for sending email through java 这是我在Java文件中使用它们通过Java发送电子邮件的地方

try {
        Message message = new MimeMessage(session);
        message.setSubject(props_load.getProperty("mailsubject"));
        message.setText(props_load.getProperty("mailtext") + "  " +generatedpwd);
        Transport.send(message);
        result = "success";
    } catch (MessagingException e) {
        result = "fail";
        logger.error("Exception Occured"+ "sendemalto" +sendemalto , e);
    }

My question is , 我的问题是

Is it possible to insert User name after the word , so that it looks like 可以在单词后面插入用户名,以便看起来像

Dear User Kiran , Following Are the credentials for REDEX Mapping Application 尊敬的用户Kiran,以下是REDEX映射应用程序的凭据

How about defining a placeholder and replacing it with your username: 如何定义一个占位符并将其替换为您的用户名:

props_load.getProperty("mailtext").replace("{{username}}", username)

With your template being like this: 与您的模板是这样的:

mailtext = Dear User {{username}},\n ...
    MessageFormat form = new MessageFormat("Dear {0}, following...");

    String userName = "Kiran";
    Object[] testArgs = {userName};

    System.out.println( form.format(testArgs) );

Output: "Dear Kiran, following..." 输出:“亲爱的基兰,关注中……”

http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html http://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html

Use either String.replace(), String.replaceFirst() or String.replaceAll(), depending on what you want to do. 使用String.replace(),String.replaceFirst()或String.replaceAll(),具体取决于您要执行的操作。

String.replace() replaces a given text with another one. String.replace()将给定的文本替换为另一个文本。 So you can just do this: 因此,您可以执行以下操作:

"Dear User, Following are...".replace("User","User "+username);

Or if you need more control use String.replaceFirst() or String.replaceAll() together with a regex. 或者,如果您需要更多控件,请使用String.replaceFirst()或String.replaceAll()以及正则表达式。 replaceFirst() only replaces the first occurrence, whereas replaceAll() replaces all occurrences: replaceFirst()仅替换第一个匹配项,而replaceAll()替换所有匹配项:

"Dear User, Following are...".replaceFirst("^Dear User"), "Dear User "+username);

Regex gives you much more control but is quite a bit more complex, so you'd need to read up on it to use it properly. 正则表达式为您提供了更多控制权,但又复杂得多,因此您需要仔细阅读它才能正确使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中的文本文件中搜索特定单词 - how to search for a certain word in a text file in java 如何使用Java搜索和突出显示某些单词? - How to search and highlight certain word with Java? 如何在Java中使用正则表达式在给定单词之后提取Java中的单词 - How to extract a word in java after a given word in using regex in java 如何在java String中查找指定单词之后的单词 - How to Find the word that comes after a specified word in java String JAVA中的单词末尾出现大写字母后如何反转单词? - How to reverse the word after getting a Capital letter at the end of the word in JAVA? Java正则表达式在字符串中某个单词前后获得2个字符串 - Java regular expression get 2 strings before and after a certain word in string 如何使用Java将bufferedImage附加/粘贴到Word或RTF文档中? - How to append/paste bufferedImage into a Word or RTF document using Java? 如何将一个单词附加到 txt 文件中,在 Java 中的正确位置(字母顺序) - How to append a word into a txt file, at the correct position (alphabetic order) in Java Java:如何将剪贴板上的图像粘贴(附加)到Word文档 - Java: How to paste (append) an image on clipboard to a word document 如何在Java中的单词模式后删除逗号 - How to remove comma after a word pattern in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM