Java 如何用特定单词替换单词
我有一个字符串:Java 如何用特定单词替换单词,java,string,Java,String,我有一个字符串: String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p> </p><p><span>Congrats!.....</span></p>"; String s=“亲爱的{customerName},您的{accountName}已激
String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p> </p><p><span>Congrats!.....</span></p>";
String s=“亲爱的{customerName},您的{accountName}已激活恭喜!…”;
因此,我想用CustomerName和accountName这两个词替换为CustomerDetails。有人能告诉我怎么更换吗。这里customerName和accountName是动态变化的。因为它们是数据库中的列,有时是不同的列。因此,我想在{and}中查找单词,并需要用列数据替换
s = s.replace("{customerName}", realCustomerName);
s = s.replace("{accountName}", realAccountNAme);
使用String的replace函数,第一个参数是要替换的字符串,第二个参数是要插入的字符串。请尝试:
s=s.replace('{customerName}',CustomerName ).replace('{accountName}',accountName);
其中
CustomerName
和accountName
将是保存客户详细信息的字符串如果您只想替换这些单词,可以执行以下操作:
s = s.replace("{customerName}", "John Doe");
s = s.replace("{accountName}", "jdoe");
String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p> </p><p><span>Congrats!.....</span></p>";
s.replace( "{customerName}", customer.getName() );
s.replace( "{accountName}", account.getName() );
String s="<p>Dear <span>%1$s, your %1$s is actived </span></p><p> </p><p><span>Congrats!.....</span></p>";
// You may also just create a new String object...
s = String.format( s, customer.getName(), account.getName() );
String s=“亲爱的{customerName},您的{accountName}已激活恭喜!…”;
s、 替换(“{customerName}”,customer.getName());
s、 替换(“{accountName}”,account.getName());
或者,如果您自己构建字符串,并且可以对其进行修改,则最好执行以下操作:
String s="<p>Dear <span>{customerName}, your {accountName} is actived </span></p><p> </p><p><span>Congrats!.....</span></p>";
s.replace( "{customerName}", customer.getName() );
s.replace( "{accountName}", account.getName() );
String s="<p>Dear <span>%1$s, your %1$s is actived </span></p><p> </p><p><span>Congrats!.....</span></p>";
// You may also just create a new String object...
s = String.format( s, customer.getName(), account.getName() );
String s=“亲爱的%1$s,您的%1$s已激活恭喜!…”;
//您也可以创建一个新的字符串对象。。。
s=String.format(s,customer.getName(),account.getName());
最后,我找到了用正则表达式替换单词的答案。这里需要替换单词b/w~,这些单词不是固定的,将从UI文本区域动态添加到字符串中
导入java.util.regex.Matcher;
导入java.util.regex.Pattern
公共类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);
}
}您是否浏览了
String
api?您没有注意到replace()
方法吗?很抱歉,customerName和accountName正在动态更改。因为这些列在数据库中有时是不同的列。在询问此问题之前,您确实应该做更多的研究。字符串有一个明显的方法。也很难阅读。我建议将这样的代码放在多行上。实际上,我需要在向客户发送邮件时替换详细信息。这些不是固定的,这些是数据库列,可能会动态更改。这是邮件模板需要发送的。因此,每次使用此代码动态更改值和字符串值时