[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppSeperateString
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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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.
//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
- include <vector>
- include <string>
- include <cassert>
//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,",',');
- ifdef THESE_WILL_FAIL_AND_SHOULD_FAIL
} { //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"); } }
- endif
Code links
- ::, scope operator
- <<, stream out operator
- #include
- algorithm(header file)
- assert
- char
- cout
- const
- endl
- for
- #include
- int
- iostream
- main
- return
- scope operator, ::
- std
- std::cout
- stream out operator, <<
External links
- http://www.richelbilderbeek.nl/CppSeperateString.htm: the source of this page. The page is also maintained better.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
