【问题标题】:String.split() --- How do I treat consecutive delimiters as one?String.split() --- 如何将连续的分隔符视为一个分隔符?
【发布时间】:2013-08-19 18:31:24
【问题描述】:

对于变量 temp 中的两个示例字符串,例如:

(1) "|RYVG|111|9|"
(2) "|RYVG|111||9|"

我想做以下事情:

String splitRating[] = temp.split("\\|",);

但我希望结果是一样的,即:

splitrating[0] = ""
splitrating[1] = "RYVG"
splitrating[2] = "111"
splitrating[3] = "9

这意味着我需要处理那个双“|”作为一个分隔符。在使用String.split() 的同时有什么方法可以做到这一点?

【问题讨论】:

    标签: java string split delimiter


    【解决方案1】:

    添加+ 以匹配管道的一个或多个实例:

    temp.split("\\|+");
    

    【讨论】:

      【解决方案2】:

      试试这个:-

      String splitRating[] = temp.split("\\|+");
      

      【讨论】:

        【解决方案3】:

        是的,有可能

         class  Split
        {
        public static void main(String[] args) 
        {
            String temp="|RYVG|111||9|";
        
            String splitRating[] = temp.split("\\|+");
        
            for(String split:splitRating){
            System.out.println(split);
        }
        }
            }
        

        【讨论】:

          【解决方案4】:

          StringUtils split 方法将连续的分隔符视为一个分隔符。

          org.apache.commons.lang.StringUtils.split("|"); 
          

          【讨论】:

            猜你喜欢
            • 2020-02-25
            • 2018-01-19
            • 2011-05-07
            • 1970-01-01
            • 2011-09-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多