I need to substitute &nbps
in TextView
. I can simply use String.replace()
, but there might be better solution?
Asked
Active
Viewed 2,101 times
4

Eugene
- 59,186
- 91
- 226
- 333
-
`String.replace()` is the only way of getting rid of these issues :) – Ali Imran Dec 12 '12 at 09:22
-
2you can set Html.fromHtml(string) to textview and check. – Sumant Dec 12 '12 at 09:30
-
1Html.fromHtml(String) works perfect for me, please answer so I can accept. – Eugene Dec 12 '12 at 09:52
-
2Unless you have other HTML that you are trying to interpret, using `fromHtml()` to replace ` ` with a space is like swatting a fly with a Buick. `replaceAll()` should be cheaper. – CommonsWare Dec 12 '12 at 12:17
2 Answers
3
try as using Pattern.compile:
Pattern p = Pattern.compile("&nbps");
String tempstr = "I love &nbps &nbps.";
Matcher matcher = p.matcher(tempstr);
String tmp = matcher.replaceAll("Android");
you can see this post about performance of String.replace and matcher.replace
String replaceAll() vs. Matcher replaceAll() (Performance differences)
-
2
-
@AliImran : i don't now about performance of this piece of code but i'm sure this will work in all cases – ρяσѕρєя K Dec 12 '12 at 09:28
-
-
I know this before but i never give it preference over simple `String.replace()` method :) – Ali Imran Dec 12 '12 at 09:31
-
@AliImran : in some cases String.replace() not worked then you have this only option . and you can see my edit answer – ρяσѕρєя K Dec 12 '12 at 09:33
-
1Ya you are right, because some times `String.replace()` not work properly. – Ali Imran Dec 12 '12 at 09:36
1
Try to use this function:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mDescription.setText(Html.fromHtml("Your Text", Html.FROM_HTML_MODE_LEGACY));
} else {
mDescription.setText(Html.fromHtml("Your Text"));
}

Mohamed AbdelraZek
- 2,503
- 4
- 25
- 36