【问题标题】:Efficient way to Parse String解析字符串的有效方法
【发布时间】:2011-08-22 10:40:22
【问题描述】:

比如说你有一个字符串,它有键值,但它之后的变量可以改变 例如:

KEY1=variable1, KEY2=variable2, KEY3=variable3

我想知道提取变量 1、变量 2 和变量 3 的最佳方法是什么。如果我知道子字符串并每次都得到它们会很好,但我不知道变量可以改变。注意键不改变

【问题讨论】:

标签: java string substring


【解决方案1】:

你可以试试这个:

String str = "KEY1=variable1, KEY2=variable2, KEY3=variable3";
String[] strArr = str.split(",");
String[] strArr2;
for (String string : strArr) {
    System.out.println(string);  // ---- prints key-value pair
    strArr2 = string.trim().split("=");
    System.out.println(strArr2[1]);  // ---- prints value
}

【讨论】:

    【解决方案2】:

    Harry 解决方案的一种变体,它将处理 , 和 = 周围的空间。

    String str = "KEY1=variable1, KEY2=variable2, KEY3=variable3    ,    a = b=1, c";
    Map<String, String> map = new LinkedHashMap<String, String>();
    for (String string : str.trim().split(" *, *")) {
        String[] pair = string.split(" *= *", 2);
        map.put(pair[0], pair.length == 1 ? null : pair[1]);
    }
    System.out.println(map);
    

    打印

    {KEY1=variable1, KEY2=variable2, KEY3=variable3, a=b=1, c=null}
    

    【讨论】:

      【解决方案3】:

      如果你想要超级高效,不需要创建不必要的对象,或者逐字符迭代,你可以使用indexOf,它比大型子字符串逐字符循环更高效。

      public class ValueFinder {
        // For keys A, B, C will be { "A=", ", B=", ", C=" }
        private final String[] boundaries;
      
        /**
         * @param keyNames To parse strings like {@code "FOO=bar, BAZ=boo"}, pass in
         *     the unchanging key names here, <code>{ "FOO", "BAZ" }</code> in the
         *     example above.
         */
        public ValueFinder(String... keyNames) {
          this.boundaries = new String[keyNames.length];
          for (int i = 0; i < boundaries.length; ++i) {
            boundaries[i] = (i != 0 ? ", " : "") + keyNames[i] + "=";
          }
        }
      
        /**
         * Given {@code "FOO=bar, BAZ=boo"} produces <code>{ "bar", "boo" }</code>
         * assuming the ctor was passed the key names <code>{ "FOO", "BAZ" }</code>.
         * Behavior is undefined if {@code s} does not contain all the key names in
         * order.
         */ 
        public String[] parseValues(String s) {
          int n = boundaries.length;
          String[] values = new String[n];
          if (n != 0) {
            // The start of the next value through the loop.
            int pos = boundaries[0].length();
            for (int i = 0; i < n; ++i) {
              int start = pos;
              int end;
              // The value ends at the start of the next boundary if
              // there is one, or the end of input otherwise.
              if (i + 1 != n) {
                String next = boundaries[i + 1];
                end = s.indexOf(next, pos);
                pos = end + next.length();
              } else {
                end = s.length();
              }
              values[i] = s.substring(start, end);
            }
          }
          return values;
        }
      }
      

      【讨论】:

        【解决方案4】:

        如果您的变量值不能包含逗号或空格,您可以简单地将字符串拆分为一个数组,使用“,”作为拆分标记。然后您可以进一步拆分等号上的每个键以检索键和值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-22
          • 2023-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-08
          相关资源
          最近更新 更多