1

我正在尝试以以下格式打印我的字符串。ua, login, login ---> ua, navigation, fault Average = 500 milliseconds. 我将 2 个字符串存储到一个名为的字符串中keyString,并将其放入由"|". 然后,当我迭代键集以获取我最初声明的格式时,我将其拆分,但它显示如下---> ua, ctiq, export|ua, ctiq export, transfer Average = 600 milliseconds。有任何想法吗?

  public static void ProcessLines(Map<String, NumberHolder> uaCount,String firstLine, String secondLine) throws ParseException
   {
    String [] arr1 = firstLine.split("-- ", 2);
    String [] arr2 = secondLine.split("-- ", 2);
    String str1 = arr1[1];
    String str2 = arr2[1];


    ......
    String keyString = str1 + "|" + str2;               
    NumberHolder hashValue = uaCount.get(keyString);
     if(hashValue == null)
     {
        hashValue = new NumberHolder();
        uaCount.put(keyString, hashValue);                      
     }
     hashValue.sumtime_in_milliseconds += diffMilliSeconds;
     hashValue.occurences++;

     public static class NumberHolder
     {
      public int occurences;
      public int sumtime_in_milliseconds;       
     }

这是打印部分

 for(String str : uaCount.keySet())
    {
        String [] arr = str.split("|",2);

        long average = uaCount.get(str).sumtime_in_milliseconds / uaCount.get(str).occurences;

        //System.out.println(str);

        System.out.println(arr[0] + " ---> " + arr[1] + " Average = " + average + " milliseconds");
    }
4

1 回答 1

1

split使用正则表达式匹配要拆分的地方,在“RegEx”中|表示 OR。要|用作文字,您需要将其转义\,在 String 中写为"\\". 或者,您可以使用[|]. 尝试

str.split("\\|",2);
于 2013-04-01T13:53:17.570 回答