[英]Replacing Strings Java
I have this function to check if some words appear in a specific line, and then surround them with a given char. 我有这个功能来检查某些单词是否出现在特定的行中,然后用给定的char包围它们。
The code above works like a charm, however since the words in the string array "words" are always low case, the words will be lower case as well. 上面的代码就像一个魅力,但是由于字符串数组中的单词“words”总是小写的,所以单词也是小写的。 How can i fix this issue ? 我该如何解决这个问题?
The inputs: 输入:
BufferedReader in = "Hello, my name is John:";
char c = '*';
String [] words = {"hello","john"};
The desired output: 所需的输出:
BufferedWriter out = "*Hello*, my name is *John*:";
The actual output: 实际输出:
BufferedWriter out = "*hello*, my name is *john*";
The code: 编码:
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();
}
}
Use 采用
line_in.replaceAll("(?i)(" + words[j] + ")", bold + "$1" + bold);
// \________________/ \/
// capture word reference it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.