There is such code:

for (User user : userList) { String oneLine = user.getDepName(); bw.write(oneLine); bw.newLine(); } 

As a result Офис "Центральный" is stored in the oneLine line. How can you replace everything " with "" , so that the Офис ""Центральный"" user.getDepName().replaceAll("\"","\"\""); did not help?

  • replaceAll returns a new string, but does not change the current one. Perhaps the problem is this. - Pavel Parshin
  • I got everything ideone.com/FxDyhh bring your code, suddenly there is an error. - Senior Pomidor
  • @PavelParshin, yes, that is the problem. Is it possible to get around this somehow so that the replacement can be performed correctly? - S.Ivanov
  • @ S.Ivanov user.setDepName(user.getDepName().replaceAll("\"", "\"\"")) ; - Senior Pomidor

1 answer 1

The replaceAll method returns a new string, but does not change the current one. Therefore, it is simply necessary to set a new value:

 String name = user.getDepName().replaceAll("\"","\"\""); user.setDepName(name); 

This behavior is explained by the fact that strings are immutable. From the documentation :

Strings are constant; cannot be changed after they are created

  • And why should a poor user change something? The author needed the modified string in a separate oneLine variable, so String oneLine = user.getDepName().replaceAll("\"","\"\""); that would be enough. - Roman