locked
Split a string RRS feed

  • Question

  • How do I split a string by " - ". Note it is space-dash-space. Example this string: "something - some-thing so - me-thing" should give me an array containing "something", "some-thing so", "me-thing". Any elegant solution will be appreciated. Thanks.
    Wednesday, July 18, 2012 7:31 AM

Answers

  • How do I split a string by " - ". Note it is space-dash-space. Example this string: "something - some-thing so - me-thing" should give me an array containing "something", "some-thing so", "me-thing". Any elegant solution will be appreciated. Thanks.

    It really depends on what kind of string you're using.
    std::string, C-style string, MFC CString, System::String,
    etc.

    For a std::string you can use the string::find and
    string::substr member functions.

    Something approximately like this:

    #include "stdafx.h"
    #include <string>
    #include <vector>
    #include <iostream>

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
      const string str("something - some-thing so - me-thing");
      string work(str);
      string pattern(" - ");
      string subs;
      vector<string> vs;

      string::size_type si;
      while((si = work.find(pattern)) != string::npos)
        {
        subs = work.substr(0, si);
        vs.push_back(subs);
        work.erase(0, si + 3);
        }
      vs.push_back(work);

      cout << str << endl;
      for(string::size_type n=0; n<vs.size(); ++n)
        {
        cout << vs[n] << endl;
        }

      return 0;
    }

    E&OE

    - Wayne
    • Proposed as answer by vatsa_mitr Wednesday, July 18, 2012 10:55 AM
    • Marked as answer by OrionWalli Tuesday, July 24, 2012 6:23 AM
    Wednesday, July 18, 2012 9:37 AM
  • regex is easier to write and to generalize (what if you want dashes surrounded by arbitrary amounts of whitespace to be your field splitters?):

    C:\Temp>type meow.cpp
    #include <iostream>
    #include <ostream>
    #include <regex>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main() {
        const string s("something - some-thing so - me-thing");
        const regex r(" - ");
    
        const vector<string> v(sregex_token_iterator(s.begin(), s.end(), r, -1), sregex_token_iterator());
    
        for (auto i = v.begin(), end = v.end(); i != end; ++i) {
            cout << "\"" << *i << "\"" << endl;
        }
    }
    
    C:\Temp>cl /EHsc /nologo /W4 /MTd meow.cpp
    meow.cpp
    
    C:\Temp>meow
    "something"
    "some-thing so"
    "me-thing"

    • Marked as answer by OrionWalli Tuesday, July 24, 2012 6:23 AM
    Wednesday, July 18, 2012 11:04 PM

All replies

  • How do I split a string by " - ". Note it is space-dash-space. Example this string: "something - some-thing so - me-thing" should give me an array containing "something", "some-thing so", "me-thing". Any elegant solution will be appreciated. Thanks.

    It really depends on what kind of string you're using.
    std::string, C-style string, MFC CString, System::String,
    etc.

    For a std::string you can use the string::find and
    string::substr member functions.

    Something approximately like this:

    #include "stdafx.h"
    #include <string>
    #include <vector>
    #include <iostream>

    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
      const string str("something - some-thing so - me-thing");
      string work(str);
      string pattern(" - ");
      string subs;
      vector<string> vs;

      string::size_type si;
      while((si = work.find(pattern)) != string::npos)
        {
        subs = work.substr(0, si);
        vs.push_back(subs);
        work.erase(0, si + 3);
        }
      vs.push_back(work);

      cout << str << endl;
      for(string::size_type n=0; n<vs.size(); ++n)
        {
        cout << vs[n] << endl;
        }

      return 0;
    }

    E&OE

    - Wayne
    • Proposed as answer by vatsa_mitr Wednesday, July 18, 2012 10:55 AM
    • Marked as answer by OrionWalli Tuesday, July 24, 2012 6:23 AM
    Wednesday, July 18, 2012 9:37 AM
  • regex is easier to write and to generalize (what if you want dashes surrounded by arbitrary amounts of whitespace to be your field splitters?):

    C:\Temp>type meow.cpp
    #include <iostream>
    #include <ostream>
    #include <regex>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main() {
        const string s("something - some-thing so - me-thing");
        const regex r(" - ");
    
        const vector<string> v(sregex_token_iterator(s.begin(), s.end(), r, -1), sregex_token_iterator());
    
        for (auto i = v.begin(), end = v.end(); i != end; ++i) {
            cout << "\"" << *i << "\"" << endl;
        }
    }
    
    C:\Temp>cl /EHsc /nologo /W4 /MTd meow.cpp
    meow.cpp
    
    C:\Temp>meow
    "something"
    "some-thing so"
    "me-thing"

    • Marked as answer by OrionWalli Tuesday, July 24, 2012 6:23 AM
    Wednesday, July 18, 2012 11:04 PM