string split

L

Larry

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
 
D

Donkey Hottie

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.
 
P

P. Lepin

Larry said:
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()));
}
 
L

Larry

P. Lepin said:

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())
);
}
 
J

James Kanze

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())));
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top