java Java如何将令牌转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5222240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java How to Convert Token to String?
提问by Donal.Lynch.Msc
Cant seem to figure out how to convert a Token (StringTokenizer) into a String.
似乎无法弄清楚如何将令牌(StringTokenizer)转换为字符串。
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.
我有一个 String[] 关键字,我从文本文件中读取了多行文本。StringTokenizer 用于将句子切碎,,,此时我需要将每个 Token(Token 的 String 表示)传递给一个函数进行词分析。
Is there a simple way to extract the string value from the token?
有没有一种简单的方法可以从令牌中提取字符串值?
Any help appreciated guys....
任何帮助赞赏家伙....
Great help guys thanks again!!
伟大的帮助家伙再次感谢!
采纳答案by Shawn
Use the method nextToken()
to return the token as a string
使用方法nextToken()
将令牌作为字符串返回
StringTokenizer st = new StringTokenizer("this is a test");
while (st.hasMoreTokens()) {
wordAnalysis(st.nextToken());//sends string to your function
}
回答by Edwin Buck
nextToken()
called on a StringTokenizer, already returns a String
. You don't need to convert that String
to a String
.
nextToken()
在 StringTokenizer 上调用,已经返回一个String
. 您无需将其转换String
为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
.
回答by dmcnelis
for(int i = 0; i<stringArray.length; i++) {
StringTokenizer st = new StringTokenizer(stringArray[i]);
while(st.hasMoreTokens()) {
callMyProcessingMethod(st.nextToken());
}
}
回答by Jordan Owens
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[].
StringTokenizer 现在是 Java 中的遗留类。从 Java 1.4 开始,建议对返回 String[] 的 String 使用 split() 方法。
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)