string tokenizer in Java

后端 未结 7 1321
耶瑟儿~
耶瑟儿~ 2020-12-02 20:58

I have a text file which contains data seperated by \'|\'. I need to get each field(seperated by \'|\') and process it. The text file can be shown as below :

7条回答
  •  误落风尘
    2020-12-02 21:33

    Here is another way to solve this problem

       String str =  "ABC|DEF||FGHT";
       StringTokenizer s = new StringTokenizer(str,"|",true);
       String currentToken="",previousToken="";
    
    
       while(s.hasMoreTokens())
       {
        //Get the current token from the tokenize strings
         currentToken = s.nextToken();
    
        //Check for the empty token in between ||
         if(currentToken.equals("|") && previousToken.equals("|"))
         {
            //We denote the empty token so we print null on the screen
            System.out.println("null");
         }
    
         else
         {
            //We only print the tokens except delimiters
            if(!currentToken.equals("|"))
            System.out.println(currentToken);
         }
    
         previousToken = currentToken;
       }
    

提交回复
热议问题