2016-07-23 84 views
0

我得到使用JDK 1.3 String[] t = words.split("_");一个错误的IntelliJJava的分裂在JDK 1.3

Error:(133, 51) java: cannot find symbol 
    symbol: method split(java.lang.String) 
    location: variable words of type java.lang.String 

我有因为项目才可以使用此SDK,我尝试JDK 1.4,但有许多其他错误,然后我决定用一些可以使用jdk 1.3编写的代码替换上面的代码。

这是什么功能?

+0

很抱歉,但不只是用java的旧版本,而尝试更新它(取决于其大小) – ScriptKiddy

回答

0

您将不得不使用StringTokenizer,indexOf()和substring()的组合,或者您自己创建的东西。

0

你可以使用C方法,即:自己实现它。

这里是一个可能的实现,现在返回的所有元素,可能需要一些调整:

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++; 
    } 
+0

认为他应该更容易使用StringTokenizer,尽管我不是100%确定的,因为我从来不需要使用旧的JDK并使用那个而不是split()方法。 – Memfisto

+0

@Memfisto如果他不想自己实现它,对他来说可能会更容易。我刚刚完成了他的问题的实施并编辑了我的评论。 –

1

public String[] split(String regex)introduced in Java 1.4

所以,你可以使用使用StringTokenizer(String str, String delim)自己实现这是introduced in Java 1.0

List list = new ArrayList(); 
StringTokenizer st = new StringTokenizer("this_is_a_test", "_"); 

while (st.hasMoreTokens()) { 
     list.add(st.nextToken()); 
} 
//[this, is, a, test] 

此外,如果你想最终结果作为一个数组,你可以使用

String[] t = list.toArray(new String[0]); 
+0

谢谢,但它说泛型不支持 – Ahmad

1

下面这段代码似乎对我来说工作得很好。

但是,我认为您需要分割的分隔符只是一个字符。

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; 
} 

替代的解决方案:

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; 
} 
0

也许一个简单的解决方案是:

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++; 
    }