【问题标题】:Parse a string and get certain values解析字符串并获取某些值
【发布时间】:2013-06-29 17:50:38
【问题描述】:

我有一个这样的字符串:

_id:2 thread_id:189 address:0292 m_size:null person:0 date:1372494272447 date_sent:0 protocol:0 read:1 status:-1 type:1 reply_path_present:0 subject:null 身体:好的。回复消息。 service_center:051108 locked:0 sim_id:0 error_code:0 seen:1 _id:1 thread_id:189 address:292 m_size:null person:0 date:1372493695831 date_sent:0协议:空读取:1 状态:-1 类型:2 回复路径存在:空主题:空 正文:测试消息 服务中心:空锁定:0 sim_id:0 错误代码:0 看到:0

我只想从整个字符串中检索该字符串的一部分,例如 address:0292body:xyz。我想要一个非常大的字符串中这两个的所有实例(上面只是一个示例)。假设它有超过 20000 个字符。

我怎样才能做到这一点?

【问题讨论】:

  • 使用 StringTokenizer。大约 20K 个字符的东西并没有那么大,仍然低于 100K。即使对其进行标记,它仍然会占用 1/4 兆内存。
  • 示例代码 pliss :p 我在考虑使用 split 然后搜索数组,但这听起来不是最好的方法。

标签: android string design-patterns


【解决方案1】:

看起来每个address 后面都跟着m_size,所以使用string.split() 函数,拆分关键字address,然后选择string.substring()(从结果数组中的每个字符串)直到到达关键字m_size。并为关键字bodyservice_center 重复整个过程。我想不出别的办法了。

【讨论】:

    【解决方案2】:

    你是对的,它看起来并不漂亮。但它有效:)

    String[] splitString = string.split(" ");
    for (int i = 0; i < splitString.length; i++) {
    if (splitString[i].startsWith("body") || splitString[i].startsWith("address"))
        Log.i(TAG, "Found: " + splitString[i]);
        // Do whatever you need to do
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2022-01-18
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多