string split

had to work out a way of splitting a string into seperate words,
couldn’t find a solution on the forum so thought id post.

  
  
vector<string> ofxStringUtil :: split ( const string& s, const string& delim, const bool keep_empty )   
{  
	vector<string> result;  
	if(delim.empty())  
	{  
		result.push_back( s );  
		return result;  
	}  
	  
	string::const_iterator substart = s.begin(), subend;  
	while (true)  
	{  
		subend = search(substart, s.end(), delim.begin(), delim.end());  
		string temp(substart, subend);  
		  
		if( keep_empty || !temp.empty() )   
		{  
			result.push_back(temp);  
        }  
		  
		if( subend == s.end() )  
		{  
			break;  
		}  
		  
		substart = subend + delim.size();  
	}  
	  
	return result;  
}  
  

the above method will return a vector which you can then iterate through to get the seperate words.

you can split the string by any delimeter you chose.

L.

hey

since 006 you have ofSplitString in ofUtils : )

ha! my bad :slight_smile:
i use the api on the wiki like a bible… will make sure to check the source also next time.