String.split() — How do I treat consecutive delimiters as one?

后端 未结 4 2058
南笙
南笙 2021-01-02 00:12

For two sample strings in variable temp such as these:

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

I want to do the following:



        
相关标签:
4条回答
  • 2021-01-02 00:27

    StringUtils split method considers the consecutive delimiters as one delimiter.

    org.apache.commons.lang.StringUtils.split("|"); 
    
    0 讨论(0)
  • 2021-01-02 00:30

    Try this:-

    String splitRating[] = temp.split("\\|+");
    
    0 讨论(0)
  • 2021-01-02 00:47

    Add a + to match one or more instances of the pipe:

    temp.split("\\|+");
    
    0 讨论(0)
  • 2021-01-02 00:52

    Yes it is possible

     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);
    }
    }
        }
    
    0 讨论(0)
提交回复
热议问题