-
Jul 25th, 2005, 01:47 PM
#1
Thread Starter
Hyperactive Member
Parsing Delimited Strings
I use the follwing method to parse delimited strings so I can get certain pieces:
Code:
public static String instanceSearch(String strIn, int intInstance, String strDelimiter)
{
String strToReturn = "";
StringTokenizer t = new StringTokenizer(strIn,strDelimiter);
int intCounter = 0;
while(t.hasMoreTokens())
{
intCounter++;
if(intCounter == intInstance)
{
strToReturn = t.nextToken().toString();
continue;
}
t.nextToken();
}
return strToReturn;
}//End instanceSearch
I call it using:
strBAN = instanceSearch(strLineIn,4,",");
This means I want the fourth item in the comma-delimeted string I call strLineIn.
For this string:
123,Bedon,Joe,21 ROCK PILE,NUEVO,CA,925679086,111,100.00,07/07/2005,1676268503
I would return 21 ROCK PILE
For this string:
456,,,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520
I return NM
I can not figure out why it is skipping the 2 empty items after the 456.
Anyone have an idea?
Last edited by rockies1; Jul 26th, 2005 at 08:46 AM.
Reason: Closing
-
Jul 26th, 2005, 08:34 AM
#2
Dazed Member
Re: Parsing Delimited Strings
Seems like a lot just to get a piece of a String. Why not just do?
Code:
public String getSub(String s, int x, int y){
return s.substring(x,y);
}
-
Jul 26th, 2005, 08:38 AM
#3
Dazed Member
Re: Parsing Delimited Strings
Try calling your method with
"456,,,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520"
then this
"456, , ,62 RIVERSIDE PLAZA,Podunk,NM,871204306,111,81.00,06/30/2005,1675376520"
-
Jul 26th, 2005, 08:40 AM
#4
Thread Starter
Hyperactive Member
Re: Parsing Delimited Strings
Because the length of the string changes every time and substring requires a specific starting point.
I need the Nth item in a delimited list.
-
Jul 26th, 2005, 08:43 AM
#5
Thread Starter
Hyperactive Member
Re: Parsing Delimited Strings
I can't change the values in the string as I am getting them from a vendor-provided file and loading them to oracle.
I ended up re-writing the method to this:
Code:
public static String instanceSearch(String strIn, int intInstance, String strDelimiter)
{
String strToReturn = "";
String strTempHolder = "";
String strTempIn = strIn + strDelimiter;
int intCounter = 0;
while(intCounter < intInstance)
{
intCounter++;
strTempHolder = strTempIn.substring(0,strTempIn.indexOf(strDelimiter));
if(intCounter == intInstance)
{
strToReturn = strTempHolder;
break;
}
strTempIn = strTempIn.substring(strTempIn.indexOf(strDelimiter)+1);
}
return strToReturn;
}//End instanceSearch
It works, but is way less efficient, I believe.
-
Jul 26th, 2005, 08:45 AM
#6
Dazed Member
Re: Parsing Delimited Strings
Posted by rockies1
Because the length of the string changes every time and substring requires a specific starting point.
I need the Nth item in a delimited list.
Understandable.
-
Jul 26th, 2005, 08:48 AM
#7
Dazed Member
Re: Parsing Delimited Strings
Your new method works. Less efficient i don't know. Id really have to take a look at it.
-
Jul 26th, 2005, 09:54 AM
#8
Dazed Member
Re: Parsing Delimited Strings
How about this?
Code:
public static String getSub(String str, int i, String del){
Scanner s = new Scanner(str).useDelimiter(del);
int index = 0;
while(index < i){
s.next();
++index;
}
return s.next();
}
-
Jul 26th, 2005, 11:08 AM
#9
Thread Starter
Hyperactive Member
Re: Parsing Delimited Strings
Looks good but, unfortunately, I am stuck using java 1.4.2 and Scanner appeared in 1.5.
Thank you for your help on this, I appreciate it!
-
Jul 26th, 2005, 11:23 AM
#10
Dazed Member
Re: Parsing Delimited Strings
Too bad not up to date. I used 1.3 for a long time, didn't see a need to jump to 1.4.
Last edited by Dilenger4; Jul 26th, 2005 at 02:02 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|