Java 替换括号中的单词
我有一个关于替换单词的问题。我有一些字符串,每个字符串如下所示: String=“今天是(快乐的)一天,我想(探索)更多关于Java的知识。” 我需要替换带括号的单词。我想用“好”替换“快乐”,用“学习”替换“探索” 我有一些想法,但我不知道怎么做Java 替换括号中的单词,java,string,replace,Java,String,Replace,我有一个关于替换单词的问题。我有一些字符串,每个字符串如下所示: String=“今天是(快乐的)一天,我想(探索)更多关于Java的知识。” 我需要替换带括号的单词。我想用“好”替换“快乐”,用“学习”替换“探索” 我有一些想法,但我不知道怎么做 for (int i = 0; i <= string.length(), i++) { for (int j = 0; j <= string.length(), j++ if ((string.charAt(i)== '(
for (int i = 0; i <= string.length(), i++) {
for (int j = 0; j <= string.length(), j++
if ((string.charAt(i)== '(') && (string.charAt(j) == ')')) {
String w1 = line.substring(i+1,j);
string.replace(w1, w2)
}
}
}
for(int i=0;i您可以运行一个从0到length-1的循环,如果循环遇到一个变量(然后将其索引分配给一个temp1变量。现在继续,只要遇到该变量)。将其索引分配给temp2。现在您可以使用string.replace替换该子字符串(string.substring(temp1+1,temp2),“您想要的字符串”)).无需使用嵌套循环。最好使用一个循环,在找到左括号和右括号时存储索引,并将其替换为单词。继续相同的循环并存储下一个索引。当您替换同一字符串中的单词时,它会更改您需要维护字符串副本并在不同字符串上执行循环和替换的字符串长度,不要使用嵌套for循环。搜索出现的(
和)
。获取这两个字符之间的子字符串,然后用用户输入的值替换它。这样做,直到没有更多的(
和)
组合剩余
import java.util.Scanner;
public class ReplaceWords {
public static String replaceWords(String s){
while(s.contains(""+"(") && s.contains(""+")")){
Scanner keyboard = new Scanner(System.in);
String toBeReplaced = s.substring(s.indexOf("("), s.indexOf(")")+1);
System.out.println("Enter the word with which you want to replace "+toBeReplaced+" : ");
String replaceWith = keyboard.nextLine();
s = s.replace(toBeReplaced, replaceWith);
}
return s;
}
public static void main(String[] args) {
String myString ="today is a (happy) day, I would like to (explore) more about Java.";
myString = replaceWords(myString);
System.out.println(myString);
}
}
Matcher
的appendReplacement
和appendTail
方法就是为此而设计的。您可以使用正则表达式扫描您的模式——一对括号,中间有一个单词——然后执行所有需要执行的操作来确定要替换它的字符串。看
一个示例,基于javadoc中的示例。我假设你有两种方法,replacement(word)
告诉你要用什么来替换这个词(这样在你的例子中,replacement(“happy”)
将等于“good”
),以及hasplacement(word)
告诉你这个词是否有替换
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher m = p.matcher(source);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String word = m.group(1);
String newWord = hasReplacement(word) ? replacement(word) : m.group(0);
m.appendReplacement(sb, newWord); // appends the replacement, plus any not-yet-used text that comes before the match
}
m.appendTail(sb); // appends any text left over after the last match
String result = sb.toString();
这段代码对我很有用,只需加载带有替换项的HashMap
,然后迭代:
import java.util.*;
public class Test
{
public static void main(String[] args) {
String string = "today is a (happy) day, I would like to (explore) more about Java.";
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("\\(happy\\)", "good");
hm.put("\\(explore\\)", "learn");
for (Map.Entry<String, String> entry : hm.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
string = string.replaceAll(key, value);
}
System.out.println(string);
}
}
import java.util.*;
公开课考试
{
公共静态void main(字符串[]args){
String=“今天是(快乐的)一天,我想(探索)更多关于Java的知识。”;
HashMap hm=新的HashMap();
hm.put(“\\(快乐)”,“好”);
hm.put(“\\(探索)”,“学习”);
对于(Map.Entry:hm.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
string=string.replaceAll(键,值);
}
System.out.println(字符串);
}
}
请记住,replaceAll
接受一个正则表达式,因此您希望它显示“\(word\)”
,这意味着斜杠本身必须转义。使用下面的代码替换字符串
String string = "today is a (happy) day, I would like to (explore) more about Java.";
string = string.replaceAll("\\(happy\\)", "good");
string = string.replaceAll("\\(explore\\)", "learn");
System.out.println(string);`
string=string.replaceAll(“(快乐)”,“良好”).replaceAll(“(探索)”,“学习”)代码>字符串在Java中是不可变的,因此该方法返回新值。它不会更新现有值,因为它不能。使用string=string。替换(w1,w2)
。无需使用嵌套循环。最好使用一个循环,在找到左括号和右括号时存储索引,并将其替换为单词。继续相同的循环并存储下一个索引。当你替换同一个字符串中的单词时,它会改变字符串的长度,你需要维护字符串的副本并在不同的字符串上执行循环和替换,@alfasin代码不好。您可能打算使用,因为使用了正则表达式,其中()
具有特殊含义,因此代码不会替换文本(happy)
,而只替换文本happy
,保留()
。除非要替换,否则可能重复或不调用appendReplacement()
,例如,if(hasplacement(word)){m.appendReplacement(sb,replacement(word));}
。javadoc中描述的“append position”独立于find()
方法,因此您不必在find()
循环中调用appendReplacement()
。可以通过让regex模式匹配目标关键字来提高性能,如中所做的。