Separate strings into words (multiple separators are specified)
Public class commandtokenizer implements java. util. Enumeration
{
Private int currentposition;
Private int newposition;
Private int maxposition;
Private string STR;
Private string delimiters [] = {"", "\ r", "\ n", "\ t "};
Private Boolean retdelims;
Private Boolean delimschanged;
Private int currentdelims =-1;
Private char maxdelimchar;
Public commandtokenizer (string STR, string delim [], Boolean returndelims)
{
Currentposition = 0;
Newposition =-1;
Delimschanged = false;
This. Str = STR;
Maxposition = Str. Length ();
Delimiters = delim;
Retdelims = returndelims;
}
Public commandtokenizer (string STR, string delim [])
{
This (STR, delim, false );
}
Public commandtokenizer (string Str)
{
Currentposition = 0;
Newposition =-1;
Delimschanged = false;
This. Str = STR;
Maxposition = Str. Length ();
Retdelims = false;
}
Private Boolean matchdelims (INT position, string s_delim)
{
If (s_delim = NULL)
Throw new nullpointerexception ();
Boolean matched = true;
If (Position + s_delim.length ()> = maxposition)
{
Return false;
}
If (Position + s_delim.length ()> maxposition) return false;
For (INT I = 0; I {
If (Str. charat (Position + I )! = S_delim.charat (I ))
{
Matched = false;
Break;
}
}
Return matched;
}
Private Boolean matchdelims (INT position)
{
If (delimiters = NULL | delimiters. Length = 0)
Throw new nullpointerexception ();
Currentdelims =-1;
Boolean matched = false;
For (INT I = 0; I {
Matched = matchdelims (Position, delimiters [I]);
If (matched)
{
Currentdelims = I;
Break;
}
}
Return matched;
}
Private int skipdelimiters (INT startpos)
{
Int position = startpos;
While (! Retdelims & position <maxposition ){
If (matchdelims (position ))
{
Position = Position + delimiters [currentdelims]. Length ();
Break;
}
Position ++;
}
Return position;
}
private int scantoken (INT startpos)
{< br>
int position = startpos;
while (position
{< br>
If (matchdelims (position)
{< br>
break;
}< br>
position ++;
}< br>
If (retdelims & (startpos = position ))
{
If (matchdelims (position)
position = Position + delimiters [currentdelims]. length ();
}< br>
return position;
}
Public Boolean hasmoretokens ()
{
Newposition = skipdelimiters (currentposition );
Return (newposition <maxposition );
}
Public String nexttoken ()
{
Currentposition = (newposition> = 0 &&! Delimschanged )?
Newposition: skipdelimiters (currentposition );
Delimschanged = false;
Newposition =-1;
If (currentposition> = maxposition)
Throw new java. util. nosuchelementexception ();
Int start = currentposition;
Currentposition = scantoken (currentposition );
Return Str. substring (START, currentposition );
}
Public String nexttoken (string delim [])
{
Delimiters = delim;
Delimschanged = true;
Return nexttoken ();
}
Public Boolean hasmoreelements ()
{
Return hasmoretokens ();
}
Public object nextelement ()
{
Return nexttoken ();
}
Public int counttokens ()
{< br>
int COUNT = 0;
int currpos = currentposition;
while (currpos currpos = skipdelimiters (currpos);
If (currpos> = maxposition)
break;
currpos = scantoken (currpos);
count ++;
}< br>
return count;
}