How to prevent java.lang.String.split() from creating a leading empty string?

后端 未结 9 1345
天命终不由人
天命终不由人 2020-11-27 05:50

passing 0 as a limit argument prevents trailing empty strings, but how does one prevent leading empty strings?

for instance

String[] test =          


        
9条回答
  •  执念已碎
    2020-11-27 06:15

    I think there is no built-in function to remove blank string in Java. You can eliminate blank deleting string but it may lead to error. For safe you can do this by writing small piece of code as follow:

      List list = new ArrayList();
    
      for(String str : test) 
      {
         if(str != null && str.length() > 0) 
         {
             list.add(str);
         }
      }
    
      test = stringList.toArray(new String[list.size()]);
    

提交回复
热议问题