Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting a string with StringTokenizer

Status
Not open for further replies.

lowbk

Technical User
Nov 26, 2001
162
SG
i've a string that looks like
^blood pressure^ glucose visual
using StringTokenizer with ^ as deliminators, i'm able to split the string into

blood pressure
glucose visual

but if i want to split it into

blood pressure
glucose
visual

how should i modify my code?
any kind soul pls advise


String input = "^blood pressure^ glucose visual";
StringTokenizer strtok = new StringTokenizer(input,"^");
while(strtok.hasMoreTokens())
{
String aword = strtok.nextToken().trim();
System.out.println(aword);
}
 
this seems fairly strange logic to program because it only applies to the second token if it has a value of "^glucose visual". if you subdivide tokens based on a space char, you would end up with

blood
pressure
glucose
visual

your going to check for this condition yourself i.e.

if( aword.equals( "glucose visual" ) )
{
// Break this token in two
}
 
i forgot to mention that the input is not fixed.

it can be ^blood pressure^ glucose or ^glucose^ visual or glucose ^blood pressure^ visual (the terms are definitely not revolving around blood pressure, visual, glucose)

kinda tricky isn't it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top