简体   繁体   中英

Java split in jdk 1.3

I get an error for String[] t = words.split("_"); using jdk 1.3 in intelliJ

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.

What is the function for that?

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

So you could use your own implementation using StringTokenizer(String str, String delim) which was 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]

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM