Summary:
See also: Classes and Objects, StringBuffer
The StringTokenizer class is designed to parse a string to extract tokens according to delimiters.
base.StringTokenizer
Class Methods | |
Name | Description |
create( src STRING, delim STRING ) |
Returns a StringTokenizer object prepared to parse the src source string according to the delim delimiters. The delim parameter is a string that can hold one or more delimiters. |
createExt( src STRING, delim STRING, esc STRING, nulls INTEGER ) |
Same as create(), except for additional options. The esc parameter defines an escape character for the delimiter. The nulls parameter indicates if empty tokens are taken into account. |
Object Methods | |
Name | Description |
countTokens() |
Returns the number of tokens left to be returned. |
hasMoreTokens() |
Returns TRUE if there are more tokens to return. |
nextToken() |
Parses the string and returns the next token. |
The StringTokenizer built-in class is provided to split a source string into
tokens, according to delimiters. The following code uses the
base.StringTokenizer.create()
method to create a
StringTokenizer that
will generate 3 tokens with the values "aaa", "bbb", "ccc":
01
DEFINE tok base.StringTokenizer02
LET tok = base.StringTokenizer.create("aaa|bbb|ccc","|")
The StringTokenizer can take a unique or multiple delimiters into account. A delimiter is always one character long. In the following example, 3 delimiters are used, and 4 tokens are extracted:
01
DEFINE tok base.StringTokenizer02
LET tok = base.StringTokenizer.create("aaa|bbb;ccc+ddd","|+;")
If you create a StringTokenizer with the base.StringTokenizer.create(src,delim)
method, the empty tokens
are not taken into account, and no escape character is defined for the
delimiters:
nextToken()
method will never return NULL strings.If you create a StringTokenizer with the base.StringTokenizer.reateExt(src,delim,esc,nulls)
method, you can configure the StringTokenizer:
When passing a character to the esc parameter, the delimiters can be escaped in the source string.
When passing TRUE to the nulls parameter, the empty tokens are taken into account:
nextToken()
method might return NULL strings.Note that when you want to specify a backslash as a delimiter, you must use double backslashes in both the source string and as the delimiter, as shown in Example 3 below.
The countTokens()
method counts the
number of tokens left to be returned.
The hasMoreTokens()
method returns
TRUE if there are more tokens to return.
The nextToken()
method parses the string
and returns the next token.
01
MAIN02
DEFINE tok base.StringTokenizer03
LET tok = base.StringTokenizer.create("/home/tomy","/")04
WHILE tok.hasMoreTokens()05
DISPLAY tok.nextToken()06
END WHILE07
END MAIN
01
MAIN02
DEFINE tok base.StringTokenizer03
LET tok = base.StringTokenizer.createExt("||\\|aaa||bbc|","|","\\",TRUE)04
WHILE tok.hasMoreTokens()05
DISPLAY tok.nextToken()06
END WHILE07
END MAIN
01
MAIN02
DEFINE tok base.StringTokenizer03
LET tok = base.StringTokenizer.create("C:\\My Documents\\My Pictures","\\")04
WHILE tok.hasMoreTokens()05
DISPLAY tok.nextToken()06
END WHILE07
END MAIN