semantics of ofSplitString

I’m not sure if this is a bug or a feature, but the semantics of ofSplitString are counter intuitive.

The “problem” is that it doesn’t account for empty items. Here is a Processing example:

  
  
String items = "a,,b,,c";  
String[] splitItems = split(items, ',');  
println(splitItems.length);  
  

The output is ‘5’.

Here is an OF example:

  
  
string items = "a,,b,,c";  
vector<string> splitItems = ofSplitString(items, ",");  
cout << splitItems.size() << endl;  
  

The output is ‘3’.

It would be great for OF to match Processing on this one, I think.

Here is a replacement snippet that matches the Processing semantics, and is simpler:

  
  
vector<string> ofSplitString(const string &source, char separator) {  
    vector<string> elements;  
    stringstream stream(source);  
    string item;  
    while(getline(stream, item, separator)) {  
        elements.push_back(item);  
    }  
    return elements;  
}  
  

It is not backwards compatible because it uses a char instead of a string for the separator… but does anyone really use multiple separators?

edit: And for more ways to split a string than you ever cared about, see http://stackoverflow.com/questions/236129/how-to-split-a-string

hey kyle, all

i would love to see a version of ofSplitString that would accomodate multiple delim’s. have some vague ideas how to do it, but – could some of you shed some light on this? right now i can only use a single whitespace as delim…

btw, i am using it in a simple and nice namegenerator addon i wrote in processing a while ago, and using it now in of. ( ofxNameGen -> available here -> https://github.com/s373/ofxNameGen )

let me know if you think of something cool to implement multiple delims like = “, {}()±”.;’?!: . )~ºª´`0123456789";

thanks

A