2

I'm looking for ways to convert text with spaces and symbols in html in android,

For Example:

"Hi this is my text" to "Hi%20this%20is%20my%20text"

Also with symbols...

Anyone knows any easy way to do it?

What I find is to convert html text to plain text.

Thanks in Advance......

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
Sergio76
  • 3,835
  • 16
  • 61
  • 88

2 Answers2

5

What you need is not HTML but URL encoding, use URLEncoder.encode, see http://developer.android.com/reference/java/net/URLEncoder.html

For example, if your write

String s = URLEncoder.encode("Hi this is my text", "UTF-8");

variable s will contain Hi%20this%20is%20my%20text

dnet
  • 1,411
  • 9
  • 19
  • 1
    @IgorGanapolsky no, first of all, this is still URL, not HTML encoding. MHT files contain the archive of a web page in MIME format, so converting it require something that could parse that, which includes some browsers. If you want to do it programmatically, see https://stackoverflow.com/questions/3230305/how-to-read-or-parse-mhtml-mht-files-in-java – dnet Nov 06 '17 at 09:28
1

Use TextUtils.htmlEncode() for this purpose.

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103