【问题标题】:android unicode to readable stringandroid unicode 到可读字符串
【发布时间】:2012-09-20 08:15:48
【问题描述】:

当我从网页阅读一些文本时,我在 TextView 中显示 unicode 字符时遇到一些问题。

我正在使用以下代码检索网页内容:

try {
    HttpGet request = new HttpGet();
    request.addHeader("User-Agent", USER_AGENT);
    request.setURI(new URI(wwwlink));
    try {
        response4 = httpClient.execute(request);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
} catch (URISyntaxException e) {e.printStackTrace();}   
try {
    in2 = null;
    String UTF8 = "UTF-8";
    in2 = new BufferedReader (new InputStreamReader(response4.getEntity().getContent(),UTF8));
} catch (IllegalStateException e) {Log.i(tag,e.toString());
} catch (IOException e) {Log.i(tag,e.toString());}

我正在阅读的页面有这个 HTML 标题标签:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

现在的问题是:我阅读了行和一些我需要的文本包含这样的 unicode 字符:

20 \u00b0C (20 degree symbol C )

我正在尝试将其转换并在 TextView 中显示为度数符号。

以下工作正常

textview.settext("\u00b0");

但是当我这样做时,该行包含 unicode 字符:

line = in2.readln;
textview.settext(line);

TextView 将显示 f.e.: some text \u00b0 some text

我已经用模拟器和手机检查了一切。

【问题讨论】:

  • 我很确定这是重复的。在 SO 上搜索。
  • 找不到它......就像在 python string.decode -> 我认为同样的问题。

标签: java android string unicode textview


【解决方案1】:

由于您的输入文本包含 unicode 的 java 表示形式,因此您需要手动替换此类字符以进行更正。这里我举一个例子,如何从字符串中替换一个字符,只是为了给出一个粗略的想法:

    String input = "some text \\u00b0 some text";
    Scanner scanner =  new Scanner(input);
    String unicodeCharStr = scanner.findWithinHorizon("\\\\{1}u[0-9a-fA-F]{4}", 0);
    char unicodeChar = (char)(int)Integer.valueOf(unicodeCharStr.substring(2, 6), 16);
    input = input.replace(unicodeCharStr, unicodeChar+"");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2021-10-16
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多