0

I am using the classic XML as my web service back end for my two apps, one written in pure Android, the other in Flutter (iOS). The Flutter version with it's unescape() function builtin has no problems deciphering the hidden newlines in the XML. However, with many years of history and thousands of programmers and solutions around, I am not able to show new lines in my text in the app with the widely available solutions like

str.replaceAll("\\r\\n", \r\n");
str.replaceAll("\\n", "\n");

Also, because I am getting unicode in string, I tried this too to show in HTML

str.replace("\r\n", "<br />");
str.replace(" ", "&nbsp;");
txtView.setText(Html.fromHtml(str));

None of these work, and I am plagued with this problem for a while now. Any experts here that can help me out?

Vijay Kumar Kanta
  • 1,111
  • 1
  • 15
  • 25

2 Answers2

0

I found Another problem like yours check below URL

replace `\\r` with `\r` in string

With replaceAll you would have to use .replaceAll("\\r", "\r"); because

to represent \ in regex you need to escape it so you need to use pass \ to regex engine.
but and to create string literal for single \ you need to write it as "\".

Clearer way would be using replace("\r", "\r"); which will automatically escape all regex metacharacters.

Jigar
  • 104
  • 7
  • I cannot change the response, as it works flawlessly in Flutter/Dart, and there was nothing unique I wrote, I am simply appending a string coming from an SQL result. – Vijay Kumar Kanta Dec 26 '18 at 06:41
0

after working on straight web service with no unicode decoding, I was able to read the new lines in the regex

(\\s{2})

which basically means the new lines are arriving as two spaces. Of course doing the following has fixed my problem

str.replaceAll("(\\s{2})", "\n\n");

for straight ASCII/UTF-8 text and the following for Unicode for world languages

str.replaceAll("(\\s{2])", "<br /><br />");

It's not the best solution, but has cleared my problem.

Vijay Kumar Kanta
  • 1,111
  • 1
  • 15
  • 25