1

I am relatively new to java programming. How would you split following lines of Strings separated by semicolons?

String; String; String; String, String; String;;String;
String; String; String; ;String;String;String;String

I would like to pass each String as an argument into a constructor (from txt file), but I am unable to find passable regex for the purpose. The constructor has 8 String arguments (from where there is nothing between two semicolons ;;, I would like to get an empty String). In this case two separate objects would be created. I am aware of how splitting a String generally works but this one seems to be too tricky for me.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Laarup
  • 31
  • 1
  • 1
  • 4
    Use the split method of String with the semicolon as separator – Stultuske Dec 02 '15 at 10:39
  • 4
    Why does it seem tricky? What are you trying to achieve and where are you stuck? Also, you have a comma in the first line (`String, String`) – Wiktor Stribiżew Dec 02 '15 at 10:40
  • Well. I tried (";"), ("\\W") and (\\;) regular expressions but I always get java.lang.ArrayIndexOutOfBoundsException. – Laarup Dec 02 '15 at 10:48
  • @stribizhev Pls read the question again "The constructor has 8 String arguments (from where there is nothing between two semicolons ;;, I would like to get an empty String)" Your code doesn't return the last argument in the first case the OP has provided, `tokens.length == 7` for that case – Ferrybig Dec 02 '15 at 11:14
  • @ferrybig: That is not mine, that is Anordil's answer. Yes, the last empty element is omitted. I posted my answer where I am describing how to avoid omitting trailing empty elements. – Wiktor Stribiżew Dec 02 '15 at 11:44
  • @Laarup: Please consider reviewing the current answers and accepting the one that works for you best, upvoting all answers that turned out useful to you. – Wiktor Stribiżew Mar 10 '17 at 14:26

3 Answers3

4

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... If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n 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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
3

Your answer is in the javadoc.

String toto = "A;B;C;D";

String[] tokens = toto.split(";");
Anordil
  • 160
  • 8
1

Since you stated that you want to contain the spaces, only split on the ; and want to keep 8 arguments for your constructor, we are going to use the split with a limit method.

String.split(String,int)

Example:

String in = "String; String; String; String, String; String;;String;";
String[] s1 = in.split(";");

Gives:

["String"," String"," String, String"," String"," String","","String"]

What is only 7 in length and will fail your constructor.


String[] s = in.split(";",8);

Gives:

["String"," String"," String"," String, String"," String","","String",""]`

What is 8 in length and will work.


You can then address your constructor using:

YourObject obj = new YourObject(s[0],s[1],s[2],s[3],s[4],s[5],s[6],s[7]);
Ferrybig
  • 18,194
  • 6
  • 57
  • 79