I am relatively new to java programming. How would you split following lines of Strings separated by semicolons?
String; String; String; String, String; String;;
The issue is the String.Split does not keep the trailing empty elements:
Trailing empty strings are therefore not included in the resulting array.
To include them, use -1
as the second argument (see demo):
String s = "String; String; String; String, String; String;;String;";
System.out.println(Arrays.toString(s.split(";", -1)));
See this Java reference:
public String[] split(String regex, int limit)
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array... Ifn
is non-positive then the pattern will be applied as many times as possible and the array can have any length. Ifn
is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.