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

后端 未结 9 1369
天命终不由人
天命终不由人 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 06:12

    I think you shall have to manually remove the first empty string. A simple way to do that is this -

      String string, subString;
      int index;
      String[] test;
    
      string = "/Test/Stuff";
      index  = string.indexOf("/");
      subString = string.substring(index+1);
    
      test = subString.split("/"); 
    

    This will exclude the leading empty string.

提交回复
热议问题