亲爱的网友,你能搜到本文中,说明您很希望了解这个问题,以下内容就是我们收集整理的相关资料,希望该答案能满足您的要求

1. StringTokennizer是Java API中的一个类,可以将一个字符串分解成多个小的字符串,用于字符串的分割操作,它是Java中很常用的类之一。

2. 在使用StringTokenizer类的时候,必须先通过构造方法创建它的对象,如下所示:

```java

StringTokenizer st = new StringTokenizer(\"This is a test string\");

```

此时,st对象就包含了被分解的字符串,使用默认的分隔符,默认情况下StringTokenizer使用空格、制表符、回车和换行作为分隔符。

3. 对于被分解的字符串,StringTokenizer提供了几种不同的方法来遍历其中的每一个分隔符分割出来的数据块以及每一个分割出来的数据块中的每个单独字符。其中最为核心和常用的方法是nextToken(),它的作用是返回被分解后下一个位于分隔符后面的数据块。

```java

//遍历所有分隔符分割出来的数据块

while (st.hasMoreTokens()) {

String token = st.nextToken();

System.out.println(token);

}

//遍历每个分割出来的数据块中的每个单独字符

while (st.hasMoreElements()) {

String token = st.nextToken();

for (int i = 0; i < token.length(); i++) {

System.out.println(token.charAt(i));

}

}

```

4. StringTokenizer还提供了另一种构造方法,可以自定义分隔符。例如,如果我们想根据逗号分隔一个字符串,可以使用以下代码:

```java

StringTokenizer st = new StringTokenizer(\"This, is, a, test, string\", \",\");

```

这样,st对象就会根据逗号分隔字符串,并返回被逗号分隔开的各个数据块。

5. 使用StringTokenizer进行字符串的分隔操作可以使程序的编写变得更加简单、高效。下面通过一个示例来进一步说明。

```java

public class TestStringTokenizer {

public static void main(String[] args) {

String str = \"This is a test string\";

StringTokenizer st = new StringTokenizer(str);

while (st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

}

}

```

运行上面的程序,输出结果为:

```

This

is

a

test

string

```

总之,StringTokenizer类是Java中一个十分实用的字符串分隔工具,通过它我们可以轻松地对字符串进行分隔操作。需要注意的是,StringTokenizer并不是线程安全的,如果在多线程的环境下使用它,需要进行线程安全的处理。同时,由于Java 5.0之后提供了更强大的字符串分隔类String.split(),建议在新的代码中使用String.split()方法实现字符串的分隔操作。

1. Introduction

A string is a sequence of characters that can be manipulated by programs. The Java programming language provides several classes for handling strings, including the StringTokenizer class. The StringTokenizer class is used for dividing a string into smaller parts called tokens. The tokens can be separated by a delimiter, which is a character or a set of characters that divide the string. This article will explain the usage of the StringTokenizer class in Java programming language.

2. Syntax

The syntax for creating an instance of the StringTokenizer class is as follows:

```

StringTokenizer st = new StringTokenizer(\"string\");

```

The parameter \"string\" is the string that needs to be divided into tokens. The delimiter used by default is the space character \" \". If no delimiter is specified, the default delimiter is used.

3. Tokenization

Tokenization is the process of dividing a string into smaller parts called tokens. Tokens can be words, numbers, or any other type of sequence that is meaningful to the program. The StringTokenizer class provides a simple way of tokenizing a string.

Consider the following example:

```

String s = \"Java is a programming language\";

StringTokenizer st = new StringTokenizer(s);

while(st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

```

In this example, the string \"Java is a programming language\" is divided into tokens using the default delimiter, which is the space character. The tokens are then printed out one by one using the `nextToken()` method of the `StringTokenizer` class. The output of this program would be:

```

Java

is

a

programming

language

```

4. Specifying Delimiters

The default delimiter used by the StringTokenizer class is the space character \" \". However, it is possible to specify a different delimiter to use. To specify a delimiter, the following syntax can be used:

```

StringTokenizer st = new StringTokenizer(\"string\", \"delimiter\");

```

The parameter \"delimiter\" is the delimiter that separates the tokens. For example, to use the comma character as a delimiter, the following code can be used:

```

String s = \"apple,banana,grape,orange\";

StringTokenizer st = new StringTokenizer(s, \",\");

while(st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

```

In this example, the string \"apple,banana,grape,orange\" is divided into tokens using the comma character as a delimiter. The output of this program would be:

```

apple

banana

grape

orange

```

5. Using Multiple Delimiters

It is also possible to use multiple delimiters when tokenizing a string. To use multiple delimiters, the following syntax can be used:

```

StringTokenizer st = new StringTokenizer(\"string\", \"delimiter1 delimiter2\");

```

In this syntax, the parameter \"delimiter1 delimiter2\" is a string containing multiple delimiters separated by spaces. For example, to use both the comma and the period characters as delimiters, the following code can be used:

```

String s = \"The quick brown fox. Jumps over the lazy dog,\";

StringTokenizer st = new StringTokenizer(s, \",. \");

while(st.hasMoreTokens()) {

System.out.println(st.nextToken());

}

```

In this example, the string \"The quick brown fox. Jumps over the lazy dog,\" is divided into tokens using the comma, period, and space characters as delimiters. The output of this program would be:

```

The

quick

brown

fox

Jumps

over

the

lazy

dog

```

6. Counting Tokens

The number of tokens in a string can be counted using the `countTokens()` method of the `StringTokenizer` class. The `countTokens()` method returns the number of tokens that are available in the string. For example:

```

String s = \"This is a test string\";

StringTokenizer st = new StringTokenizer(s);

System.out.println(st.countTokens());

```

In this example, the string \"This is a test string\" is divided into tokens using the default delimiter, which is the space character. The `countTokens()` method is then used to count the number of tokens in the string. The output of this program would be:

```

5

```

7. Conclusion

不知这篇文章是否帮您解答了与标题相关的疑惑,如果您对本篇文章满意,请劳驾您在文章结尾点击“顶一下”,以示对该文章的肯定,如果您不满意,则也请“踩一下”,以便督促我们改进该篇文章。如果您想更进步了解相关内容,可查看文章下方的相关链接,那里很可能有你想要的内容。最后,感谢客官老爷的御览