How to convert a string with special characters into utf8?

Asked

Viewed 227 times

-2

I’m not getting to decode the characters that appear in this string with Dart.

import 'dart:convert';

void main(){

var myString="Data de coleta superior à 15 dias não permitida.";

var newString = utf8.decode(myString);

print(newString);   //Retorno desejado: Data de coleta superior há 15 dias não é permitida.

}

1 answer

0

You can use the package dart:html to do what you want, I was able to play what you want by removing the &amp, see if my solution helps you.

Another thing is that your String was with some wrong codes according to the expected result... Fixed to:

"Data de coleta superior há 15 dias não &eacute permitida."

Here’s an example of how the code looks:

import 'dart:html' as html;

void main(){

  var myString="Data de coleta superior há 15 dias não &eacute permitida.";

  print(_parseHtmlString(myString));
}

String _parseHtmlString(String htmlString) {
  var text = html.Element.span()..appendHtml(htmlString);
  return text.innerText;
}

You can test here on Dartpad.

Source: Decode HTML encoded text in Dart.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.