Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string split

Reply
Thread Tools

string split

 
 
Larry
Guest
Posts: n/a
 
      01-18-2010
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

 
Reply With Quote
 
 
 
 
Donkey Hottie
Guest
Posts: n/a
 
      01-18-2010
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.
 
Reply With Quote
 
 
 
 
P. Lepin
Guest
Posts: n/a
 
      01-18-2010

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:air<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:air<std::string, std::string>(
std::string(str.begin(), str.begin() + match),
std::string(str.begin() + match + delim.length(),
str.end()));
}

--
P. Lepin
 
Reply With Quote
 
Larry
Guest
Posts: n/a
 
      01-18-2010

"P. Lepin" <(E-Mail Removed)> ha scritto nel messaggio
news:hj1a6f$ohq$(E-Mail Removed)...
>
> 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())
);
}

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-18-2010
On Jan 18, 7:57 am, "Larry" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments