1
You’re making a mistake on a replace
what I’m doing.
The string in question is this:
<table id='tablecritica' class='table table-bordered table-hover'> <tbody> <tr> <td> Valor Contábil (R$) </td> <td style='text-align: right;'> 1.076,63 </td> </tr> <tr class='fundo total table-secondary'> <td> Total A (R$) </td> <td> 1.076,63 </td> </tr> <tr style='font-weight: bold'> <td> Comparativo </td> <td style='text-align: right;'> <> </td> </tr> <tr> <td> Base de Cálculo (R$) </td> <td style='text-align: right;'> 0,00 </td> </tr> <tr> <td> Isenta Não Tributada (R$) </td> <td style='text-align: right;'> 0,00 </td> </tr> <tr> <td> Outras (R$) </td> <td style='text-align: right;'> 1.076,63 </td> </tr> <tr> <td> Imposto Retido ST (R$) </td> <td style='text-align: right;'> 0,00 </td> </tr> <tr> <td> Imposto Retido Subs. ST (R$) </td> <td style='text-align: right;'> 0,00 </td> </tr> <tr> <td> Outros Impostos (R$) </td> <td style='text-align: right;'> 0,00 </td> </tr> <tr class='fundo total table-secondary'> <td> Total B (R$) </td> <td> 1.076,63 </td> </tr> </tbody> </table>
I am sending this script as a parameter to an HTML template, where I read the HTML and only replicate the parameters.
Hashtable valuesReplace = new Hashtable();
valuesReplace.put("Tabelacritica", gb.nullToEmpty( tabelaCritica ));
Hashtable hashReplace = (Hashtable) valuesReplace;
String templateEmail = gb.getPropertyAplication("Path") + "/" + templateName;
StringBuilder stringBuilder = new StringBuilder();
java.io.File file = new java.io.File(templateEmail);
java.io.FileInputStream fileInputStream = new java.io.FileInputStream( file );
java.io.BufferedReader bufferedReader = new java.io.BufferedReader( new java.io.InputStreamReader(fileInputStream) );
while ( ( row = bufferedReader.readLine() ) != null ) {
row = new String(row.trim().getBytes(), "UTF-8");
stringBuilder.append( row );
}
String contentEmail = stringBuilder.toString();
String str;
java.util.Enumeration keys = hashReplace.keys();
while(keys.hasMoreElements()) {
str = (String) keys.nextElement();
contentEmail = contentEmail.replaceAll("\\{" + str + "\\}", hashReplace.get(str).toString());
}
If I send any other parameter as string works normally.
Some tips not directly related to
replace
:Hashtable
is obsolete, prefer to useHashMap
(or any other implementation ofMap
). Next time, try to keep the example more brief, only with the necessary (for example, your code has read from a file that we don’t know what it is, and doesn’t seem relevant to the problem) - try to make a [mcve] read this link, has many tips to make an example that does not depend on external resources (such as files) and helps a lot who answer...– hkotsubo
... in this case it was possible to know the cause of the problem, but it is not always so (if the problem was in the contents of the file, for example, no one could simulate the error). That’s why it’s important to try to make a [mcve]
– hkotsubo