string split

Discussion in 'C++' started by Larry, Jan 18, 2010.

  1. Larry

    Larry Guest

    Hi,

    I am dealing with the http header. it is basically made up of pairs like
    teh following:

    key: value\n\r
    key: value\n\r
    key: value\n\r

    Now, I need to capture those values and push them into an associative array,
    so basically I am going to use std::string this way:

    #include <string>
    #include <map>
    using namespace std;

    map<string, string> header;

    Yet, since I am quite new to std::string I'd like to know how to split one
    line based on ": ", then I am goit to remove any extra whitespaces by using
    my own chomp()

    thanks
     
    Larry, Jan 18, 2010
    #1
    1. Advertisements

  2. On 18.1.2010 9:57, Larry wrote:
    > Hi,
    >
    > I am dealing with the http header. it is basically made up of pairs
    > like teh following:
    >
    > key: value\n\r
    > key: value\n\r
    > key: value\n\r
    >
    > Now, I need to capture those values and push them into an associative
    > array, so basically I am going to use std::string this way:
    >
    > #include <string>
    > #include <map>
    > using namespace std;
    >
    > map<string, string> header;
    >


    In the Java libraries it is defined (I think) as

    map<String, List<String>> header

    That it because there may be more that one values for each key.

    --
    You possess a mind not merely twisted, but actually sprained.
     
    Donkey Hottie, Jan 18, 2010
    #2
    1. Advertisements

  3. Larry

    P. Lepin Guest

    Larry wrote:
    > Yet, since I am quite new to std::string I'd like to know how to split one
    > line based on ": ", then I am goit to remove any extra whitespaces by
    > using my own chomp()


    Er.

    std::pair<std::string, std::string> splitInTwo(
    std::string str, std::string delim) {
    std::string::size_type match = str.find(delim);
    assert(match != std::string::npos);
    return std::pair<std::string, std::string>(
    std::string(str.begin(), str.begin() + match),
    std::string(str.begin() + match + delim.length(),
    str.end()));
    }

    --
    P. Lepin
     
    P. Lepin, Jan 18, 2010
    #3
  4. Larry

    Larry Guest

    "P. Lepin" <> ha scritto nel messaggio
    news:hj1a6f$ohq$...
    >
    > P. Lepin


    thanks this rocks!

    #include <iostream>
    #include <string>
    #include <map>
    #include <stdlib.h>
    #include <assert.h>
    using namespace std;

    void chomp(string&);
    pair<string,string> mysplit(string,string);

    int main()
    {
    map<string,string> hash;

    string line = "key: value\r\n";
    chomp(line);

    hash.insert(mysplit(line, ": "));

    for(map<string,string>::const_iterator it=hash.begin(); it!=hash.end();
    ++it)
    {
    cout << it->first << "->" << it->second << endl;
    }

    system("pause");
    return EXIT_SUCCESS;
    }

    void chomp(string& szString)
    {
    string whitespaces(" \t\f\v\n\r");
    size_t found;

    found=szString.find_last_not_of(whitespaces);
    if (found!=string::npos)
    szString.erase(found+1);
    else
    szString.clear();
    }

    pair<string,string> mysplit(string str, string delim)
    {
    string::size_type match = str.find(delim);
    assert(match != string::npos);

    return pair<string,string>(
    string(str.begin(), str.begin() + match),
    string(str.begin() + match + delim.length(), str.end())
    );
    }
     
    Larry, Jan 18, 2010
    #4
  5. Larry

    James Kanze Guest

    On Jan 18, 7:57 am, "Larry" <> wrote:

    > I am dealing with the http header. it is basically made up of
    > pairs like teh following:


    > key: value\n\r
    > key: value\n\r
    > key: value\n\r


    > Now, I need to capture those values and push them into an
    > associative array, so basically I am going to use std::string
    > this way:


    > #include <string>
    > #include <map>
    > using namespace std;


    > map<string, string> header;


    > Yet, since I am quite new to std::string I'd like to know how
    > to split one line based on ": ", then I am goit to remove any
    > extra whitespaces by using my own chomp()


    Have you looked at std::find? Something like:

    std::map< std::string, std::string >::value_type
    split( std::string const& line )
    {
    std::string::const_iterator pivot
    = std::find(line.begin(), line.end(), ':');
    return std::map< std::string, std::string >::value_type(
    std::string( line.begin(), pivot ),
    (pivot == line.end()
    ? std::string()
    : std::string(pivot + 1, line.end())));
    }

    --
    James Kanze
     
    James Kanze, Jan 18, 2010
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    512
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    803
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    271
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    351
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    786
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page