如何在Java中处理字符串中不位于行尾的 “`\n“` 字符?

huangapple 未分类评论44阅读模式
英文:

How to escape ```\n``` char which is not at the end of the line of the string using java?

问题

String str = "Name, Age, Gender, Description\n"
           + "Test1, 12, M, Hello World\n"
           + "Test2, 21, M, Bye\\\\nWorld";
英文:

I have a csv string. A sample would be ->

String str = "Name, Age, Gender, Description\n"
           + "Test1, 12, M, Hello World\n"
           + "Test2, 21, M, Bye\nWorld";

When I open this in a csv viewer, Bye\nWorld is creating an issue. How do I escape the \n char in Bye\nWorld but not the ones that are at the end of each data using java? The logic should not detect the \n char at the end of each line, it should only detect the ones that are in the data. An example of the output I want is

Name, Age, Gender, Description
Test1, 12, M, Hello World
Test2, 21, M, Bye\\nWorld

This output will create a correct csv for this data.

huangapple
  • 本文由 发表于 2020年7月23日 21:32:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63055524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定