dimanche 1 mars 2015

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.


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

}
}

Aucun commentaire:

Enregistrer un commentaire