【问题标题】:java - String hard code working but inputted not workingjava - 字符串硬代码工作但输入不工作
【发布时间】:2014-11-15 17:47:02
【问题描述】:

我的问题是,如果我使用字符串硬编码,答案是正确的,但如果我使用输入的字符串不起作用。 例如:

 String variable = " \u0020\uFEB3\uFEE8\uFB93\u0020\uFEBB\uFE92\uFEEE\u0631\u0020";
 TextView show = (TextView) findViewById(R.id.preshow);
 show.settext(variable );

textview 显示:صنگ صبور

但是:

  File filematn = new File(Environment.getExternalStorageDirectory()+File.separator+"SingingStudio/"+songname+"/"+songname+"file.txt");
    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(filematn));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
        br.close();
    }
    catch (IOException e) {
        //You'll need to add proper error handling here
    }
    String variable = text.toString();
    TextView show = (TextView) findViewById(R.id.preshow);
    show.settext(variable );

文本视图显示:\u0020\uFEB3\uFEE8\uFB93\u0020\uFEBB\uFE92\uFEEE\u0631\u0020

我该如何解决它。谢谢你

【问题讨论】:

  • 解析出来并转换成Unicode。

标签: java android


【解决方案1】:

当您读取文件时,反斜杠会被前面的反斜杠转义。解决这个问题并不像看起来那么简单,但在这个 SO 问题中有所涉及:How to replace \\u by \u in Java String

【讨论】:

  • 出于好奇,你用什么方法去掉了反斜杠?
  • 模式 unicode = Pattern.compile("\\\\u(.{4})");匹配器 matcher = unicode.matcher("aaa\\u2022bbb\\u2014ccc"); StringBuffer sb = new StringBuffer(); while (matcher.find()) { int code = Integer.parseInt(matcher.group(1), 16); matcher.appendReplacement(sb, new String(Character.toChars(code))); } matcher.appendTail(sb);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多