在不使用String.split()的情况下拆分字符串 - 并返回分隔符

时间:2014-10-01 20:28:49

标签: java regex string split

String s = "ab#cd#ef#gh#";
String regex = "#";

char [] sChar = s.toCharArray();
char [] regexChar = regex.toCharArray();

int count = 1;
for (int i = 0; i < regexChar.length; i++){
    for (int j = 0; j < sChar.length; j++){
        if (regexChar [i] == sChar[j]){
            count += 2;
        }
    }
}

String [] splitS = new String [count];
String temp;
for (int k = 0; k < count; k++){
    temp = "";
    for (int i = 0; i < regexChar.length; i++){
        for (int j = 0; j < sChar.length; j++){
            if (regexChar[i] == sChar[j]){
                temp = Character.toString(regexChar[i]);
            }
            else {
                temp = temp + Character.toString(sChar[j]);
            }
        }
    }
    splitS[k] = temp;
}

这是我的代码到目前为止,它给了我splitS[#,#,#,#,#,#,#,#]。但它应该是splitS[ab,#,cd,#,ef,#,gh,#]。谁能告诉我它为什么要这样做?

2 个答案:

答案 0 :(得分:2)

我认为应该认真考虑使用现有标准课程的评论,但只是为了好玩,那么:

import java.util.ArrayList;

class Splitter{
    static ArrayList<String> tokenize(String subject, String pattern)
    {
        ArrayList<String> tokens = new ArrayList<>();

        int tokenOff = 0;
        while(true)
        {
            int tokenPos = subject.indexOf(pattern, tokenOff);
            if(tokenPos == -1){ break; }
            String tok = subject.substring(tokenOff, tokenPos);

            addToken(tokens, tok);
            addToken(tokens, pattern);

            tokenOff = (tokenPos + pattern.length());
        }
        // Add any remaining characters
        addToken(tokens, subject.substring(tokenOff));

        return tokens;
    }

    static void addToken(ArrayList<String> list, String tok)
    {
        if(tok.length() > 0){ list.add(tok); }
    }

    public static void main(String args[])
    {
        String subject, pattern;
        ArrayList<String> tokens;

        subject = "ab#cd#ef#gh#"; 
        pattern = "#";
        tokens = tokenize(subject, pattern);
        System.out.println(tokens); // [ab, #, cd, #, ef, #, gh, #]

        subject = "ab##cd##ef##gh##"; 
        pattern = "##";
        tokens = tokenize(subject, pattern);
        System.out.println(tokens); // [ab, ##, cd, ##, ef, ##, gh, ##]

        subject = "ab##cd##ef##gh##ij"; 
        pattern = "##";
        tokens = tokenize(subject, pattern);
        System.out.println(tokens); // [ab, ##, cd, ##, ef, ##, gh, ##, ij]

        subject = "ab##cd#ef#gh##ij"; 
        pattern = "##";
        tokens = tokenize(subject, pattern);
        System.out.println(tokens); // [ab, ##, cd#ef#gh, ##, ij]
    }
}

答案 1 :(得分:1)

你需要什么。摆脱了所有不必要和不安全的废话。

public static String[] split(String s, String regex) {
    List<String> result = new ArrayList<>();
    int beginning = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.substring(i).startsWith(regex)) {
            // if you need splitter in output array
            if (!result.isEmpty()) {  
                result.add(regex);
            }
            result.add(s.substring(beginning, i));

            // Move forward for splitter size
            i += regex.length();
            beginning = i;
        }
    }

    // Optionally if you really need an array instead
    String[] splitS = new String[result.size()];
    result.toArray(splitS);
    return splitS;
}