【问题标题】:Wrong string parsing from remote service来自远程服务的错误字符串解析
【发布时间】:2018-06-21 16:34:17
【问题描述】:

任意字符串来自远程服务。

根据Body:子字符串的存在,我必须以各种方式解析它,我这样做:

    String longString = service.getString();

    if (longString.indexOf("Body:") != -1) {
        // some code
    } else {
        // enother code
    }

但是当字符串进入并且看起来像Body:\Dsdqwe .... 时,逻辑从else 块运行。我该如何解决?

【问题讨论】:

  • 在 if 之前添加一个打印语句,以打印 longString。如果字符串是 "Body:\...",这不应该转到 else 块
  • 这是一个很长的String,但我只需要关注Body: 子字符串。也许\D会造成问题?它在else 块中打印Body:\Dsdqwe...
  • 通常不会。索引应该是 0 而不是 -1
  • 难道\D这个字符可以去掉前面的字符或者破坏前面的行?
  • 没有。转义字符转义后面的字符。它不会(不应该)影响他们面前的事情

标签: java string parsing


【解决方案1】:

改用这个 -

String longString = service.getString();

if (longString.contains("Body:")) {
    // some code
} else {
    // enother code
}

【讨论】:

  • String.contains 在后台使用String.indexOf
  • @DenisVabishchevich 它不会解决您的问题,但瑞诗凯诗有道理。毕竟,您并不关心它在字符串中的哪个位置,您只是在验证“Body:”是否在您的字符串中,从而使 contains(String param) 可能更适合
【解决方案2】:

我觉得你可以试试这个,

String longString = service.getString().replaceAll("\","");
if (longString.indexOf("Body:") != -1) {
        // some code
    } else {
        // enother code
    }

【讨论】:

  • 你的意思是“replaceAll”吗?
  • 可能比longString.replaceAll("\","\\"); 更好,但它可能会对longString 造成性能问题
  • @DenisVabishchevich 是的,但如果你想这样做: String test = "Body:\D";它不会编译,所以很可能,'\' 把它搞砸了。不过,我会期待另一个错误消息,但仍然
【解决方案3】:

这是一个有趣的问题。

我创建了以下名为“strings.txt”的文本文件:

Hallo Welt
Body:
Body:Content
Body:\Dasdf

还有一个读取每个字符串并根据您的检查对其进行测试的小方法:

public void foobar()
{
    // As the character \D is an invalid escape sequence, you can not hard code it without using double \.
    // But using double \ would change the actual input as in the process, so read the data from a file.
    List<String> stringList = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader("strings.txt"))) {
        String line;
        while ((line = br.readLine()) != null) {
            stringList.add(line);
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }

    // All strings are read from the file, now process and test each one of them
    for(String str : stringList ) {

        if (str.indexOf("Body:") != -1) {
            System.out.println("'" + str + "' - Is containing the search term");
        } else {
            System.out.println("'" + str + "' - Is _NOT_ containing the search term");
        }

    }
}

这是我在运行上述代码时得到的输出:

'Hallo Welt' - Is _NOT_ containing the search term
'Body:' - Is containing the search term
'Body:Content' - Is containing the search term
'Body:\Dasdf' - Is containing the search term

结论: 如果你的程序接收到类似Body:\Dasdf 的字符串,它可以被正确处理并且没有任何问题。问题的根源一定在其他地方。

以下只是一些想法,问题可能来自哪里:

  • 编码:源以与接收机器不同的编码发送字符串。因此接收方混淆了字符,字符串比较失败。
  • 编译:可能由于代码错误或 IDE 配置错误,您的源代码尚未编译

您可以执行的步骤,以验证几件事:

  • 检查你的代码是否真的编译过
  • 您是否使用序列化?如果是,请检查可能的编码问题。
  • 关于编码:不要相信你的 IDE,它可能会为你转换东西。将接收到的原始字符串写入一个简单的文本文件,然后使用 Notepad 或 Notepad++ 等文本编辑器打开该文件以检查编码。

【讨论】:

  • 相当广泛,但从某种意义上说,这篇文章是评论,而不是答案
  • 这篇文章太长了,评论里的代码格式也不是很好。
猜你喜欢
  • 1970-01-01
  • 2014-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 2018-12-16
  • 1970-01-01
相关资源
最近更新 更多