我有一个函数来检查某个单词是否出现在一个特定的行中,然后用一个给定的字符包围它们。
上面的代码工作起来很有魅力,但是由于字符串数组中的单词“word”总是小写,所以单词也是小写的。我怎样才能解决这个问题?
投入:
BufferedReader in = "Hello, my name is John:";
char c = '*';
String [] words = {"hello","john"}; 期望的输出:
BufferedWriter out = "*Hello*, my name is *John*:";实际产出:
BufferedWriter out = "*hello*, my name is *john*";守则:
public void replaceString(BufferedReader in, BufferedWriter out, char c, String[] words){
String line_in = in.readLine();
while (line_in != null) {
for (int j = 0; j < words.length; j++) {
line_in = line_in.replaceAll("(?i)" + words[j], bold + words[j]
+ bold);
}
out.write(line_in);
out.newLine();
line_in = in.readLine();
}
}发布于 2015-03-01 17:58:59
使用
line_in.replaceAll("(?i)(" + words[j] + ")", bold + "$1" + bold);
// \________________/ \/
// capture word reference ithttps://stackoverflow.com/questions/28796987
复制相似问题