Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java替换字符串_Java_String_Replace_Buffer_Lowercase - Fatal编程技术网

用Java替换字符串

用Java替换字符串,java,string,replace,buffer,lowercase,Java,String,Replace,Buffer,Lowercase,我有这个函数来检查某些单词是否出现在特定的行中,然后用给定的字符将它们包围起来 上面的代码很有魅力,但是由于字符串数组“words”中的单词总是小写,因此单词也将是小写。如何解决此问题 输入: BufferedReader in = "Hello, my name is John:"; char c = '*'; String [] words = {"hello","john"}; 所需输出: BufferedWriter out = "*Hello*, my name is *John*

我有这个函数来检查某些单词是否出现在特定的行中,然后用给定的字符将它们包围起来

上面的代码很有魅力,但是由于字符串数组“words”中的单词总是小写,因此单词也将是小写。如何解决此问题

输入:

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();

    }
}
public void replaceString(BufferedReader-in,BufferedWriter-out,char c,String[]words){
字符串行_in=in.readLine();
while(行_in!=null){
for(int j=0;j
使用

line_in.replaceAll("(?i)(" + words[j] + ")", bold + "$1" + bold);
//                      \________________/           \/
//                         capture word          reference it