[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppSeperateString

(C++) Seperate a std::string into multiple std::strings, seperated by a seperator

The original source of the code below can be found here.
It is also maintained better.


The functions below take a string and a seperator (i.e. a comma) and return a std::vector of std::strings. In main the function is tested thoroughly using assert. And sure, you can also just copy the functions.

  1. include <vector>
  2. include <string>
  3. include <cassert>
//From http://www.richelbilderbeek.nl std::vector<std::string> seperateString(std::string input, const char& seperator) { assert(input.empty()==false); assert(input[0]!=seperator); assert(input[input.size()-1]!=seperator); std::vector<std::string> result; int pos = 0; while(pos<static_cast<int>(input.size())) { if (input[pos]==seperator) { const std::string found = input.substr(0,pos); result.push_back(found); input = input.substr(pos+1,input.size()-pos); pos = 0; } ++pos; } result.push_back(input); return result; } //From http://www.richelbilderbeek.nl std::vector<std::string> seperateString(std::string input, const std::string& seperator) { assert(input.empty()==false); assert(input.substr(0,seperator.size()) != seperator); assert(input.substr(input.size()-seperator.size(),seperator.size())!=seperator); std::vector<std::string> result; int pos = 0; while(pos<static_cast<int>(input.size())) { if (input.substr(pos,seperator.size())==seperator) { const std::string found = input.substr(0,pos); result.push_back(found); input = input.substr(pos+seperator.size(),input.size()-pos); pos = 0; } ++pos; } result.push_back(input); return result; } //From http://www.richelbilderbeek.nl int main() { { //#define THESE_WILL_FAIL_AND_SHOULD_FAIL
          1. ifdef THESE_WILL_FAIL_AND_SHOULD_FAIL
//Empty const std::vector<std::string> v0 = seperateString("",','); const std::vector<std::string> v1 = seperateString("",","); //Seperator only const std::vector<std::string> v2 = seperateString(",",','); const std::vector<std::string> v3 = seperateString(",",","); //Single input, leading seperator const std::vector<std::string> v4 = seperateString(",a",','); const std::vector<std::string> v5 = seperateString(",a",","); //Single input, trailing seperator const std::vector<std::string> v6 = seperateString("a,",','); const std::vector<std::string> v7 = seperateString("a,",","); //Two inputs, leading seperator const std::vector<std::string> v8 = seperateString(",a,a",","); const std::vector<std::string> v9 = seperateString(",a,a",','); //Two inputs, trailing seperator const std::vector<std::string> v10 = seperateString("a,a,",","); const std::vector<std::string> v11 = seperateString("a,a,",',');
          1. endif
} { //Single input, seperator of type char const std::vector<std::string> v = seperateString("a",','); assert(v[0]=="a"); } { //Two inputs, seperator of type char const std::vector<std::string> v = seperateString("a,b",','); assert(v[0]=="a"); assert(v[1]=="b"); } { //Single input, seperator of type std::string const std::vector<std::string> v = seperateString("a",","); assert(v[0]=="a"); } { //Two inputs, seperator of type std::string const std::vector<std::string> v = seperateString("a,b",","); assert(v[0]=="a"); assert(v[1]=="b"); } { //Five inputs, seperator of type char const std::vector<std::string> v = seperateString("a,bb,ccc,dddd,eeeee",','); assert(v[0]=="a"); assert(v[1]=="bb"); assert(v[2]=="ccc"); assert(v[3]=="dddd"); assert(v[4]=="eeeee"); } { //Five inputs, seperator of type std::string const std::vector<std::string> v = seperateString("a,bb,ccc,dddd,eeeee",","); assert(v[0]=="a"); assert(v[1]=="bb"); assert(v[2]=="ccc"); assert(v[3]=="dddd"); assert(v[4]=="eeeee"); } { //Three inputs, of which one empty, seperator of type char const std::vector<std::string> v = seperateString("a, ,ccc",','); assert(v[0]=="a"); assert(v[1]==" "); assert(v[2]=="ccc"); } { //Two inputs, of which one starts with a separator, seperator of type char //Note: avoid this kind of input const std::vector<std::string> v = seperateString("a,,ccc",','); assert(v[0]=="a"); assert(v[1]==",ccc"); } { //Three inputs, of which one empty, seperator of type std::string const std::vector<std::string> v = seperateString("a, ,ccc",","); assert(v[0]=="a"); assert(v[1]==" "); assert(v[2]=="ccc"); } { //Two inputs, of which one starts with a separator, seperator of type std::string //Note: avoid this kind of input const std::vector<std::string> v = seperateString("a,,ccc",","); assert(v[0]=="a"); assert(v[1]==",ccc"); } }


Code links

External links



last edited (March 31, 2008) by bilderbikkel, Number of views: 4745, Current Rev: 10 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.