473,799 Members | 3,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parse comma delimited text string

in MS VC++ Express I need to know how to get from one comma delimited
text string to many strings.

from this:

main_string = "onE,Two,Th ree , fouR,five, six "

to these:

string1 = "one"
string2 = "two"
string3 = "three"
string4 = "four"
string5 = "five"
string6 = "six"

the white space needs to be removed and the case needs to be all upper
or lower.
The result needs to be strings and not pointers or addresses in memory
so I can test:

string test = "three";

if (three == "three")

some getlines will only have two strings some may have up to 10
Please help with examples, you input with exact syntax is greatly
appreciated

I have been struggling with not using pointers but every time I end up
using them.

Thanks in advance

Feb 7 '06
25 31804
In article <44************ @individual.net >,
"Sumit Rajan" <su*********@gm ail.com> wrote:
"Sumit Rajan" <su*********@gm ail.com> wrote in message
news:44******** ****@individual .net...
void trim(std::strin g& str)
{
std::string::si ze_type idx = str.find_first_ not_of(' ');
if (idx != std::string::np os) {
str.erase(0, idx);
}
idx = str.find_last_n ot_of(' ');
if (idx != std::string::np os) {
str.erase(idx+1 , str.size()-1);
}
}


Sorry, this function has a bug. May be simplest to use an std::istringstr eam
as you will see in Alf's post.


What bug do you see? The only bug I can find is that if 'str' is nothing
but spaces then the function doesn't do anything.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 7 '06 #11
Since your using VC++ express, are you using standard C++ or C++/CLI?
I ask becasue if your using the managed String classes, you can very
easily do what you wish as follows:

System::String^ s = "1,2,3,4,5" ;
cli::array<Syst em::String^>^ s2 = s->Split(',');
// now use s2[0]...s2[s2->Length] as needed.

If using standard C++, then the suggestions already provided by the
other posters are worth considering.

Feb 7 '06 #12
On Tue, 07 Feb 2006 11:05:54 GMT, al***@start.no (Alf P. Steinbach)
wrote:
StringVector stringsFrom( std::string s )
{
std::replace( s.begin(), s.end(), ',', ' ' );
std::istringstr eam stream( s );
StringVector result;
for( ;; )
{
std::string word;
if( !( stream >> word ) ) { break; }
result.push_bac k( uppercase( word ) );
}
return result;
}


Alternative string tokenizers can be found here:
http://groups.google.com/group/comp....8f07f527?hl=en

Best regards,
Roland Pibinger
Feb 7 '06 #13
electrixnow wrote:
the white space needs to be removed and the case needs to be all upper
or lower.


My favorite approach would be to use a filtering stream buffer
to do the clean-up and use normal string input functions otherwise:

#include <iostream>
#include <streambuf>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <stdlib.h>

char clean(char i)
{
return i == ','? ' '
: std::tolower(st atic_cast<unsig ned char>(i));
}

struct cleanbuf:
std::streambuf
{
enum { s_size = 1024 };
cleanbuf(std::s treambuf* sbuf): m_sbuf(sbuf) {}
int underflow()
{
std::streamsize size = m_sbuf->sgetn(m_buf, s_size);
setg(m_buf, m_buf, m_buf + size);
std::transform( m_buf + 0, m_buf + size, m_buf, clean);
return gptr() == egptr()
? std::char_trait s<char>::eof() : *gptr();
}
private:
std::streambuf* m_sbuf;
char m_buf[s_size];
};

int main()
{
cleanbuf sbuf(std::cin.r dbuf());
std::istream in(&sbuf);
std::vector<std ::string> strs(
(std::istream_i terator<std::st ring>(in)),
(std::istream_i terator<std::st ring>()));
std::copy(strs. begin(), strs.end(),
std::ostream_it erator<std::str ing>(std::cout, "\n"));
}
~
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 8 '06 #14

"Daniel T." <po********@ear thlink.net> wrote in message
news:po******** *************** *******@news.ea st.earthlink.ne t...
What bug do you see? The only bug I can find is that if 'str' is nothing
but spaces then the function doesn't do anything.


Precisely. My other post on this thread takes care of it.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Feb 8 '06 #15
In article <44************ @individual.net >,
"Sumit Rajan" <su*********@gm ail.com> wrote:
"Daniel T." <po********@ear thlink.net> wrote in message
news:po******** *************** *******@news.ea st.earthlink.ne t...
What bug do you see? The only bug I can find is that if 'str' is nothing
but spaces then the function doesn't do anything.


Precisely. My other post on this thread takes care of it.


I think your other post was more complicated than necessary...

str.erase( 0, str.find_first_ not_of( whitespace ) );

Takes care if it without the extra conditionals/loops.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 8 '06 #16
In article <44************ @individual.net >,
Dietmar Kuehl <di***********@ yahoo.com> wrote:
electrixnow wrote:
the white space needs to be removed and the case needs to be all upper
or lower.
My favorite approach would be to use a filtering stream buffer
to do the clean-up and use normal string input functions otherwise:


Unfortunately, your code doesn't compile on my system. I had to change
the vector<string> c_tor call to:

vector<string> strs;
copy ( istream_iterato r<string>(in), istream_iterato r<string>(),
back_inserter(s trs) );

Also, your code only works for the simple case, where there is no
whitespace *within* a string delimited by commas. How would you change
it to account for the more complicated case?

The nice thing about your code is that it does everything in a single
pass whereas the code I presented to date requires two passes (one to
call tolower, and a second to remove the commas.) It would be a simple
change to make mine one pass as well...

template < typename Out >
void fn( string str, Out it )
{
transform( str.begin(), str.end(), str.begin(), clean );
// the above uses your 'clean' function.
stringstream ss( str );
copy( istream_iterato r<string>( ss ), istream_iterato r<string>(),
it );
}

This seems much more straight forward and needs fewer lines of code.

I must say that every contribution so far has shed new light on the
problem for me and caused me to improve my code in some way. IMHO, this
is usenet at its best.
#include <iostream>
#include <streambuf>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <stdlib.h>

char clean(char i)
{
return i == ','? ' '
: std::tolower(st atic_cast<unsig ned char>(i));
}

struct cleanbuf:
std::streambuf
{
enum { s_size = 1024 };
cleanbuf(std::s treambuf* sbuf): m_sbuf(sbuf) {}
int underflow()
{
std::streamsize size = m_sbuf->sgetn(m_buf, s_size);
setg(m_buf, m_buf, m_buf + size);
std::transform( m_buf + 0, m_buf + size, m_buf, clean);
return gptr() == egptr()
? std::char_trait s<char>::eof() : *gptr();
}
private:
std::streambuf* m_sbuf;
char m_buf[s_size];
};

int main()
{
cleanbuf sbuf(std::cin.r dbuf());
std::istream in(&sbuf);
std::vector<std ::string> strs(
(std::istream_i terator<std::st ring>(in)),
(std::istream_i terator<std::st ring>()));
std::copy(strs. begin(), strs.end(),
std::ostream_it erator<std::str ing>(std::cout, "\n"));
}
~


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 8 '06 #17

"Daniel T." <po********@ear thlink.net> wrote in message
news:po******** *************** *******@news.ea st.earthlink.ne t...
I think your other post was more complicated than necessary...

str.erase( 0, str.find_first_ not_of( whitespace ) );

Takes care if it without the extra conditionals/loops.


True. This one is far more reader-friendly.

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hc ltech.com>
Feb 8 '06 #18
Daniel T. wrote:
Unfortunately, your code doesn't compile on my system. I had to change
the vector<string> c_tor call to:

vector<string> strs;
copy ( istream_iterato r<string>(in), istream_iterato r<string>(),
back_inserter(s trs) );
In this case your compiler or the standard library implementation is
broken: the standard container classes are defined to accept pairs of
input iterators as constructor arguments.
Also, your code only works for the simple case, where there is no
whitespace *within* a string delimited by commas. How would you change
it to account for the more complicated case?
This is a rather different specification which, in particular, does
not yet account for whitespace at the beginning or the end of the
string: is this whitespace to be removed or not? Since commas work
as true delimiters here, I assume it is to be included. I this case
I would [temporarily] 'imbue()' a 'std::ctype<cha r>' facet which
only considers comma and possibly newline (if this is also considered
a separator). The extractor functions use whitespaces as field
separators for strings. If the case adjustment is still necessary,
I would use the modified facet in combination with the filtering
stream buffer.
This seems much more straight forward and needs fewer lines of code.
Injecting existing filtering stream buffers into the processing is
pretty straight forward. Of course, writing them is not necessarily
so but it is not that hard either. The real advantage I see in the
filtering stream buffer over your code is that it encapsulates the
complete solution, especially if it is also equipped with a simple
input stream which automatically maintains the stream buffer.
I must say that every contribution so far has shed new light on the
problem for me and caused me to improve my code in some way.


This was my intention of posting the code...
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 8 '06 #19
Daniel T. wrote:
int (*lower)(int) = &tolower;
transform( str.begin(), str.end(), str.begin(), lower );


Note that this does *NOT* work! The argument to 'std::tolower() '
has to be an *unsigned* value. However, on platforms where 'char'
is signed, the argument could expand to a negative value! In
portable code, the only valid call to 'std::tolower(i nt)' with a
'char' looks like this:

std::tolower(st atic_cast<unsig ned char>(c));

The only possible variation is how the 'char' is first cast to
an 'unsigned char'.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 8 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2826
by: Arne | last post by:
From: "Arne de Booij" <a_de_booij@hotmail.com> Subject: Comma delimited array into DB problems Date: 9. februar 2004 10:39 Hi, I have an asp page that takes input from a form on the previous page, puts that into an array and inserts the array into SQL server. Now here is the problem:
18
12344
by: MW | last post by:
Dear All Does anyone have a regular expression to parse a comma delimited line with some fields optionally having string delimiters (text qualifiers) I am currently testing with this regular expression and it works in almost all my test cases. I found this on the internet in a C# solution. ,(?=(*"*")*(?!*")) However in some of my test cases it fails and I am having difficulty
22
872
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to tokenize the comma separated values.I used strtok function reading line by line using fgets.but it gives some weird behavior.It doesnot stripout the "" fully.Could any body have sample code for the same so that it will be helfful for my...
2
2015
by: Larry Williams | last post by:
Is there an easy way to convert a string of data to XML format without having to create the xml one field at a time? Sorry I'm a newbie and my xml knowledge is limited. I have tried to search through the documentation in .net but nothing jumps out at me. Sample code would be great!
9
4515
by: Wayne | last post by:
I have the following string: "smith", "Joe", "West Palm Beach, Fl." I need to split this string based on the commas, but as you see the city state contains a comma. String.split will spilt the city state. Is there a built in function that I'm missing that will do the split and recognize that the comma in city state is part of the item? --
5
2721
by: Yama | last post by:
Hi, I am looking to create a report comma delimited on a click of a button. Explanantion: 1. Get from the database: "SELECT * FROM Customers WHERE Region = 'CA'" 2. Use either DataReader or DataSource 3. Create a button "Export" 4. On ServerClick Event prompt user to save as a text comma delimited file.
2
2136
by: Ron | last post by:
so if my textbox is named textbox1 and my listbox is named ltsdisplay, for the button that would make this all happen I would just need to: lstdisplay.items.textbox1(delimited.Split(",".ToCharArray())) is that right? or no? I try this and I get an error...
5
64652
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
64209
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10029
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4144
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.