[英]Java split in jdk 1.3
I get an error for String[] t = words.split("_");
我收到String[] t = words.split("_");
的错误String[] t = words.split("_");
using jdk 1.3 in intelliJ 在intelliJ中使用jdk 1.3
Error:(133, 51) java: cannot find symbol
symbol: method split(java.lang.String)
location: variable words of type java.lang.String
I have to use this SDK because the project is old, I tried jdk 1.4 but had many other errors, then I decided to replace the above code with something that can be complied using jdk 1.3. 我必须使用此SDK,因为该项目很旧,我尝试了jdk 1.4,但遇到许多其他错误,然后我决定将上述代码替换为可以使用jdk 1.3进行编译的代码。
What is the function for that? 它的功能是什么?
public String[] split(String regex)
was introduced in Java 1.4 Java 1.4中引入了 public String[] split(String regex)
So you could use your own implementation using StringTokenizer(String str, String delim)
which was introduced in Java 1.0 因此,您可以使用Java 1.0中引入的 StringTokenizer(String str, String delim)
使用自己的实现。
List list = new ArrayList();
StringTokenizer st = new StringTokenizer("this_is_a_test", "_");
while (st.hasMoreTokens()) {
list.add(st.nextToken());
}
//[this, is, a, test]
Further if you want final result as an Array, you can use 此外,如果您希望最终结果作为数组,则可以使用
String[] t = list.toArray(new String[0]);
The following piece of code seems to be working fine for me. 以下代码对我来说似乎工作正常。
However, I have assumed that the delimiter on the basis of which you need to split is only a single character. 但是,我假设您需要在其基础上分割的分隔符只是一个字符。
public static void main(String[] args){
String string = ",alpha,beta,gamma,,delta";
String[] wordsSplit = splitByDelimiter(string, ",");
for(int i=0; i<wordsSplit.length; i++){
System.out.println("-"+wordsSplit[i]+"-");
}
}
public static String[] splitByDelimiter(String fullString, String delimiter){
// Calculate number of words
int index = 0;
int[] delimiterIndices = new int[fullString.length()];
int wordCount = 0;
do{
if(delimiter.equals(fullString.charAt(index)+"")){
delimiterIndices[wordCount++] = index;
}
index++;
} while(index < fullString.length());
// Correction for strings not ending in a delimiter
if(!fullString.endsWith(delimiter)){
delimiterIndices[wordCount++] = fullString.length();
}
// Now create the words array
String words[] = new String[wordCount];
int startIndex = 0;
int endIndex = 0;
for(int i=0; i<wordCount; i++){
endIndex = delimiterIndices[i];
words[i] = fullString.substring(startIndex, endIndex);
startIndex = endIndex+1;
}
return words;
}
Alternate solution: 替代解决方案:
public static ArrayList splitByDelimiter(String fullString, String delimiter){
fullString += delimiter; //
ArrayList words = new ArrayList();
int startIndex = 0;
int endIndex = fullString.indexOf(delimiter); //returns first occurence
do{
words.add(fullString.substring(startIndex, endIndex));
startIndex = endIndex+1;
endIndex = fullString.indexOf(delimiter, startIndex);
} while(endIndex != -1);
return words;
}
您将不得不使用StringTokenizer,indexOf()和substring()的组合,或者您自己制作的东西。
You could go with the C approach, which is: implement it yourself. 您可以采用C方法,即:自己实现。
Here is a possible implementation, it now returns all elements, might need some tweaks: 这是一个可能的实现,它现在返回所有元素,可能需要一些调整:
int length;
int split_amount = 0;
String temp = new String("This_takes_into_consideration_something_something_test");
char split = '_';
for(int i = 0; i<length;i++){
if(temp.charAt(i) == split ){
split_amount++;
}
}
split_amount++;
String[] result = new String[split_amount];
int j = 0;
for(int i = 0; i<split_amount; i++){
result[i] = "";
boolean t = true;
for(; j<length && t ;j++){
if(temp.charAt(j) == split){
t = false;
break;
}
result[i] += temp.charAt(j);
}
j++;
}
Maybe a simple solution is: 也许一个简单的解决方案是:
String words = "this_is_a_test";
StringTokenizer st0 = new StringTokenizer(words, "_");
String[] t = new String[st0.countTokens()];
int k = 0;
while(st0.hasMoreTokens()){
String tmp0 = st0.nextToken();
t[k] = tmp0;
k++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.