java 未定义 String 类型的 split(String) 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2724745/
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
The method split(String) is undefined for the type String
提问by pi.
I am using Pulse - the Plugin Manager for Eclipse and installed. I have the Eclipse 3.5 for mobile development(Pulsar) profile with a couple other profiles.
我正在使用 Pulse - Eclipse 插件管理器并已安装。我有用于移动开发(Pulsar)的 Eclipse 3.5 配置文件和其他几个配置文件。
I realized that the split() method called on a string from code such as below:
我意识到 split() 方法从如下代码中调用了一个字符串:
String data = "one, two, three, four";
data.split(",");
generates the error: "The method split(String) is undefined for the type String". I am aware that the split() method did not exist before Java's JRE 1.4 and perhaps could be the cause of the problem. The problem is I don't think I have jre/sdk versions installed. Perhaps there's one in-built with the Pulsar profile and needs editing - but I couldn't tell what settings (and where) needs tweaking. I have checked Windows>Preferences>Java>Installed JREs and it's set to >= jre1.4.
生成错误:“未定义 String 类型的 split(String) 方法”。我知道 split() 方法在 Java 的 JRE 1.4 之前不存在,可能是问题的原因。问题是我认为我没有安装 jre/sdk 版本。也许 Pulsar 配置文件内置了一个需要编辑 - 但我不知道哪些设置(以及哪里)需要调整。我已经检查了 Windows>Preferences>Java>Installed JREs 并将其设置为 >= jre1.4。
采纳答案by DJClayworth
I note "Eclipse 3.5 for mobile development". Maybe this tool expects to run J2ME, which I believe is several issues behind J2SE.
我注意到“用于移动开发的 Eclipse 3.5”。也许这个工具期望运行 J2ME,我认为这是 J2SE 背后的几个问题。
This pagegives links to the JavaDoc for the various APIs in JME. There are several versions, (follow the links under CLDC and CDC and look for java.lang.String) but as far as I can tell none of them define String.split().
此页面提供了指向 JME 中各种 API 的 JavaDoc 的链接。有几个版本,(按照 CLDC 和 CDC 下的链接并查找 java.lang.String)但据我所知,它们都没有定义 String.split()。
回答by niraj
String.splitmethod is introduced from Java version 1.4 onward, if you have same job to be done you may give a try to this:
String.split方法是从 Java 1.4 版本开始引入的,如果你有同样的工作要做,你可以试试这个:
public String[] splitStr(String str, String delim) {
StringTokenizer stringTokenizer = new StringTokenizer( str, delim );
String[] strArr = new String[stringTokenizer.countTokens()];
int i = 0;
while( stringTokenizer.hasMoreTokens() ) {
strArr[i] = stringTokenizer.nextToken();
}
return strArr;
}
回答by Carl Smotricz
Last time I looked (in a Windows XP installation), I found the default installed JVM to be 1.3 .
上次我查看(在 Windows XP 安装中)时,我发现默认安装的 JVM 是 1.3 。
You could pop open a "DOS shell" (err, command prompt) and type java -versionto see the truth at least about whatever Java is on the PATH.
您可以弹出一个“DOS shell”(错误,命令提示符)并键入java -version以查看至少有关PATH.
I would definitely recommend installing an up-to-date JDK. The JDK includes a compiler and other tools, that's more useful to a developer than just a JRE. You then need to go back into Eclipse's preferences and point its JDK/JRE settings at your newly installed JDK.
我绝对会推荐安装最新的 JDK。JDK 包括编译器和其他工具,这对开发人员来说比 JRE 更有用。然后,您需要返回 Eclipse 的首选项并将其 JDK/JRE 设置指向新安装的 JDK。
回答by MerajQA
String data = "one, two, three, four";
data.split(",");
String data = "one, two, three, four";
data.split(",");
are you declaring
你在声明
String[] variable
字符串[] 变量
example
String[] variable = data.split(",");for(String value: variable){System.out.println(value);
}
例子
String[] variable = data.split(",");for(String value: variable){System.out.println(value);
}
I tried it its working on it
我试过了它的工作

