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

passing 0 as the limit argument prevents the completion of blank lines, but how to prevent the prevention of leading blank lines?

eg

String[] test = "/Test/Stuff".split("/"); 

leads to an array with the parameters "," Test "," Stuff ".

Yes, I know that I can flip my own tokenizer ... but the API documents for StringTokenizer say

"StringTokenizer is an inherited class that is preserved for compatibility even though its use is not recommended in the new code. It is recommended that anyone looking for this functionality use a split"

+45
java string
Feb 22 2018-12-12T00:
source share
7 answers

It’s probably best to just remove any leading separator:

 String input = "/Test/Stuff"; String[] test = input.replaceFirst("^/", "").split("/"); 

You can make it more general by putting it in a method:

 public String[] mySplit(final String input, final String delim) { return input.replaceFirst("^" + delim, "").split(delim); } String[] test = mySplit("/Test/Stuff", "/"); 
+42
Feb 22 2018-12-12T00:
source share

Apache Commons has a utility method for this: org.apache.commons.lang.StringUtils.split

StringUtils.split ()

In fact, in our company, we now prefer to use this method for separation in all our projects.

+19
Feb 22 2018-12-22T00: 00Z
source share

I do not think you can do this with the built-in split method. So you have two options:

1) Make your own split

2) Iterating through the array after calling split and deleting empty elements

If you make your own split, you can simply combine these two options

 public List<String> split(String inString) { List<String> outList = new ArrayList<>(); String[] test = inString.split("/"); for(String s : test) { if(s != null && s.length() > 0) outList.add(s); } return outList; } 

or you can simply check if the separator is in the first position before you call split and ignore the first character if this happens:

 String delimiter = "/"; String delimitedString = "/Test/Stuff"; String[] test; if(delimitedString.startsWith(delimiter)){ //start at the 1st character not the 0th test = delimitedString.substring(1).split(delimiter); } else test = delimitedString.split(delimiter); 
+4
Feb 22
source share

I think there is no built-in function to delete an empty string in Java. You can delete an empty delete line, but this can lead to an error. For security, you can do this by writing a small piece of code as follows:

  List<String> list = new ArrayList<String>(); for(String str : test) { if(str != null && str.length() > 0) { list.add(str); } } test = stringList.toArray(new String[list.size()]); 
+1
Feb 22 '12 at 8:40
source share

You can use StringTokenizer for this purpose ...

 String test1 = "/Test/Stuff"; StringTokenizer st = new StringTokenizer(test1,"/"); while(st.hasMoreTokens()) System.out.println(st.nextToken()); 
0
Feb 22 '12 at 5:33
source share

I think you will have to manually delete the first blank line. An easy way to do this is

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

This excludes the leading empty string.

0
Feb 22 2018-12-12T00:
source share

Here's how I got around this issue. I take a string, call .toCharArray () on it to split it into an array of characters, and then skip that array and add it to the String list (wrapping each char with String.valueOf). I assume there are some trade-offs with performance, but this seems like a readable solution. Hope this helps!

  char[] stringChars = string.toCharArray(); List<String> stringList = new ArrayList<>(); for (char stringChar : stringChars) { stringList.add(String.valueOf(stringChar)); } 
0
May 18 '14 at 19:14
source share



All Articles