473,480 Members | 4,939 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

.net "split" function equivalent

Hi

I've seen that the string classes in the .net framework have a cool (new?) function: splitting strings and returning the substrings in an array. Is there any equivalent to this in "old-style" MFC C++?

Thanks
Sam
Nov 17 '05 #1
1 2461
Sam Johnson wrote:
Hi

I've seen that the string classes in the .net framework have a cool (new?)
function: splitting strings and returning the substrings in an array. Is
there any equivalent to this in "old-style" MFC C++?


If by "old-style" you mean "standard", you might do something like this:

#include <vector>
#include <string>
#include <iostream>

void split_string(const std::string& source, const std::string& delims,
std::vector<std::string>& result)
{
std::string::size_type begin(0);
std::string::size_type end(0);

while (end != std::string::npos)
{
end = source.find_first_of(delims, begin);
result.push_back(source.substr(begin, end - begin));
begin = end + 1;
}
}

int main()
{
std::vector<std::string> strings;
split_string("This |is^ a string with;[ several ]delimiters.", ";[]^|",
strings);

for (std::vector<std::string>::size_type i = 0; i < strings.size(); ++i)
{
std::cout << '"' << strings[i] << '"' << std::endl;
}
}

Tested with g++ 3.2: VC++ is on my other machine - sorry. Can't speak for
CString or whatever else might be lurking in the MFC. Apologies if I
overlooked some feature of the STL that already does this :-).
Arnold the Aardvark
http://www.codeproject.com/cpp/garbage_collect.asp

Nov 17 '05 #2

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

Similar topics

11
2478
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
4
2403
by: kinne | last post by:
The following code is supposed to reverse the date in "yyyy-mm-dd" format, but it produces different results in Firefox 1.0 and in Internet Explorer 6SP1. In Firefox, the result is correct...
0
507
by: pmarisole | last post by:
I am using the following code to split/join values in a multi-select field. It is combining all the values in All the records into one long string in each record in recordset. Example: I have a...
6
3418
by: py_genetic | last post by:
Hi, I'm looking to generate x alphabetic strings in a list size x. This is exactly the same output that the unix command "split" generates as default file name output when splitting large...
12
3295
blazedaces
by: blazedaces | last post by:
Hello again. I'm trying to take as an input an ArrayList<String> and utilize String's .spit(delimiter) method to turn that into a String. I'm getting some kind of error though (I'll post the code...
2
2342
by: elgin | last post by:
I have a split Access 2003 database. I have signed the database with a Code Signing Certificate from Small Business Server. This works fine and users can have Access macro security on high or...
7
5326
by: spoken | last post by:
Hi, I'm trying to read a file with data seperated by "|" character. Ex: 3578.27|2001|Road Bikes|Metchosin|Canada 3399.99|2001|Mountain Bikes|Pantin|France 3399.99|2001|Mountain...
2
1493
by: gabielmatos | last post by:
Good morning, I'm reading a csv file. My problem is that when I use the comma delimiter brings me more fields that have the line. This is because some fields contain commas in them. An example of...
0
7040
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,...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4478
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...
0
2994
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...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
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 ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
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...

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.