Java如何将Token转换为字符串?

发布于 2024-10-20 20:06:56 字数 233 浏览 2 评论 0原文

似乎无法弄清楚如何将令牌(StringTokenizer)转换为字符串。

我有一个 String[] 关键字,并且我从文本文件中读取了多行文本。 StringTokenizer 用于将句子切碎,,, 此时我需要将每个 Token(Token 的字符串表示形式)传递给函数进行单词分析。

有没有一种简单的方法可以从令牌中提取字符串值?

任何帮助都很感谢大家...

非常有帮助的家伙再次感谢!

Cant seem to figure out how to convert a Token (StringTokenizer) into a String.

I have a String[] of keywords, and I read in multiple lines of text from a text file.
StringTokenizer is used to chop the sentence up,,, at which point I need to pass each Token (the String representation of the Token) to a function for word analysis.

Is there a simple way to extract the string value from the token?

Any help appreciated guys....

Great help guys thanks again!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒解孤独 2024-10-27 20:06:56

使用方法 nextToken() 以字符串形式返回令牌

StringTokenizer st = new StringTokenizer("this is a test");
         while (st.hasMoreTokens()) {
             wordAnalysis(st.nextToken());//sends string to your function
         }

Use the method nextToken() to return the token as a string

StringTokenizer st = new StringTokenizer("this is a test");
         while (st.hasMoreTokens()) {
             wordAnalysis(st.nextToken());//sends string to your function
         }
梦初启 2024-10-27 20:06:56

nextToken()< /code>调用 StringTokenizer,已返回 String。您不需要将该 String 转换为 String

令牌只是较大字符串的一小部分(字符串)。令牌用于各种形式的处理,并且需要一个单词(单词令牌)来区分项目的类别和特定项目。

For example, every word, period, and comma in this string is a token.

适合以下标记(所有标记都是字符串):

For 
example
, 
every 
word
,
period
,
and 
comma 
in 
this 
string 
is 
a 
token
.

nextToken() called on a StringTokenizer, already returns a String. You don't need to convert that String to a String.

A Token is just a small (string) portion of a larger String. Tokens are used in various forms of processing, and a word (the word token) is needed to differentiate the category of items from the specific items.

For example, every word, period, and comma in this string is a token.

Would lend itself to the following tokens (all of which are strings):

For 
example
, 
every 
word
,
period
,
and 
comma 
in 
this 
string 
is 
a 
token
.
南汐寒笙箫 2024-10-27 20:06:56
for(int i = 0; i<stringArray.length; i++) {
    StringTokenizer st = new StringTokenizer(stringArray[i]);
    while(st.hasMoreTokens()) {
       callMyProcessingMethod(st.nextToken());
    }
}
for(int i = 0; i<stringArray.length; i++) {
    StringTokenizer st = new StringTokenizer(stringArray[i]);
    while(st.hasMoreTokens()) {
       callMyProcessingMethod(st.nextToken());
    }
}
药祭#氼 2024-10-27 20:06:56

StringTokenizer 现在是 Java 中的遗留类。从 Java 1.4 开始,建议对 String 使用 split() 方法,该方法返回 String[]。

http:// /download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

StringTokenizer is now a legacy class in Java. As of Java 1.4 it is recommended to use the split() method on String which returns a String[].

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文