I'm parsing an input file that has multiple keywords preceded by a +
. The +
is my delimiter in a split
, with individual tokens being written to an array. The resulting array includes a blank record in the [0]
position.
I suspect that split
is taking the "nothing" before the first token and populating project[0]
, then moving on to subsequent tokens which all show up as correct.
Documentaion says that this method has a limit
parameter:
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.
and I found this post on SO, but the solution proposed, editing out the leading delimiter (I used a substring(1)
to create a temp field) yielded the same blank record for me.
Code and output appers below. Any tips would be appreciated.
import java.util.regex.*;
import java.io.*;
import java.nio.file.*;
import java.lang.*;
//
public class eadd
{
public static void main(String args[])
{
String projStrTemp = "";
String projString = "";
String[] project = new String[10];
int contextSOF = 0;
int projStringSOF = 0;
int projStringEOF = 0;
//
String inputLine = "foo foofoo foo foo @bar.com +foofoofoo +foo1 +foo2 +foo3";
contextSOF = inputLine.indexOf("@");
int tempCalc = (inputLine.indexOf("+")) ;
if (tempCalc == -1) {
proj StrTemp = "+Uncategorized";
} else {
projStringSOF = inputLine.indexOf("+",contextSOF);
projStrTemp = inputLine.trim().substring(projStringSOF).trim();
}
project = projStrTemp.split("\\+");
//
System.out.println(projStrTemp+"\n"+projString);
for(int j=0;j<project.length;j++) {
System.out.println("Project["+j+"] "+project[j]);
}
}
CONSOLE OUTPUT:
+foofoofoo +foo1 +foo2 +foo3
Project[0]
Project[1] foofoofoo
Project[2] foo1
Project[3] foo2
Project[4] foo3