String::split into JUCE?


#1

Buenos dias,

just implemented a String split function. Since I see there's a lot of convenience functionality in String, but no split, maybe you want to include this so not everyone has to do this tedious index-fiddling again ;)

// declaration (default param)
std::vector<String> splitString(const String& input,
                                const char delimiter,
                                bool skipEmpty=false);

// implementation
std::vector<String> splitString(const String& input,
                                const char delimiter,
                                bool skipEmpty) {
    std::vector<String> outVector;
    int lastIndex = 0;
    for(int index = 0 ; index < input.length() ; ++index) {
        String substring;
            
        if(input[index] == delimiter) {
            substring = input.substring(lastIndex, index);
            lastIndex = index+1;
            if(skipEmpty && substring == "") {}
            else
                outVector.push_back(substring);
        }
        if(index+1 >= input.length()) {
            substring = input.substring(lastIndex, index+1);
            if(skipEmpty && substring == "") {}
            else
                outVector.push_back(substring);
        }            
    }
    
    return outVector;
}

For performance reasons you might want to switch to the getCharPointer interface, for me it's fine like this.

Best, Patric


#2

I think you were probably looking in the wrong place - there are a bunch of methods for doing this in StringArray, e.g. StringArray::addTokens, fromTokens, etc


#3

Ahh, well then, nevermind! blush