Author Message

<  beginners  ~  string split

PostPosted: Wed Dec 23, 2009 8:04 am
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
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.

Code:
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.



_________________
http://julapy.com/blog
Offline Profile
PostPosted: Wed Dec 23, 2009 10:10 am
Site AdminJoined: Wed Apr 11, 2007 1:02 amPosts: 1192Location: barcelona
hey

since 006 you have ofSplitString in ofUtils : )


Offline Profile
PostPosted: Wed Dec 23, 2009 10:47 am
User avatarJoined: Mon Jan 19, 2009 7:02 amPosts: 214Location: sydney
ha! my bad :)
i use the api on the wiki like a bible... will make sure to check the source also next time.



_________________
http://julapy.com/blog
Offline Profile

Display posts from previous:  Sort by:

All times are UTC
Page 1 of 1
3 posts
Users browsing this forum: No registered users and 1 guest
Search for:
Post new topic  Reply to topic
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
cron