Separate strings into words (multiple separators are specified)

Source: Internet
Author: User
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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.