473,796 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split string contain special token?

Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which
algorithms function is suitable.
Thanks !
Alex 2005.01.12

Jul 22 '05 #1
15 6796
ze*******@gmail .com wrote:
Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which
algorithms function is suitable.
Thanks !

You might try the new Boost String Algorithm library:

http://www.boost.org/doc/html/string....html#id576776

Jonathan
Jul 22 '05 #2
I know boost string can split such string literal,but I want to
implement 'split' using stl.

Thanks your reply!

Jul 22 '05 #3

<ze*******@gmai l.com> wrote in message
I know boost string can split such string literal,but I want to
implement 'split' using stl.


How about this -
string Str = "alex delia john";
std::istringstr eam iss( Str );
std::vector<str ing> vec( (std::istream_i terator<string> (iss)),
std::istream_it erator<string>( ) );

HTH,
Sharad
Jul 22 '05 #4
ze*******@gmail .com wrote:
I know boost string can split such string literal,but I want to
implement 'split' using stl.
For homework, I guess?
Thanks your reply!


You're welcome.

Jonathan
Jul 22 '05 #5
NO!
I just want to write a helper function for my workmates.

Jul 22 '05 #6

<ze*******@gmai l.com> skrev i en meddelelse
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
NO!
I just want to write a helper function for my workmates.

The best thing would be to give your workmates a boost using boost. Here you
get well-documented, tested code. Much better than anything you can expect
yourself to deliver.

/Peter
Jul 22 '05 #7
> How about this -
string Str = "alex delia john";
std::istringstr eam iss( Str );
std::vector<str ing> vec( (std::istream_i terator<string> (iss)),
std::istream_it erator<string>( ) );

#include <string>
#include <vector>
#include <sstream>
int main(int, char**)
{
std::string Str = "alex de-lia jo-hn";
std::istringstr eam iss( Str ); // iss is a stream, that has the data
of Str
std::vector<std ::string> my_tokens(
(std::istream_i terator<std::st ring>(iss)),
std::istream_it erator<std::str ing>()
);
for (int i=0; i<my_tokens.siz e(); ++i)
{
printf("%s\n", my_tokens[i].c_str());
}
}

I don't get it quite. Especially this part:

(std::istream_i terator<std::st ring>(iss)),
std::istream_it erator<std::str ing>());

the first is an iterator to the begin of the string - what is the
second one? How does the spitting occour? Can I use different
seperators? Like ",-." ?
That's a terrific source code!
-Gernot
Jul 22 '05 #8
ze*******@gmail .com wrote:
Hi, all:
I want to implement a function (using std::string and std::vector)
split a string contain special token into vector.
eg:
string = "alex delia john"

==>

vector[0] = "alex";
vector[1] = "delia";
vector[2] = "john";

I want use some stl algorithms library function,but I don't kown which algorithms function is suitable.


This comes up fairly frequently. You can either use stringstream
approach or something like the utility I posted here a few times. See
the link below:

http://groups-beta.google.com/group/...9335f1c396d288

I would have reposted the code, but I'm stuck using google for a while
to post and it messes up code indentation.

Brian

Jul 22 '05 #9
Peter Koch Larsen wrote:
<ze*******@gmai l.com> skrev i en meddelelse
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
NO!
I just want to write a helper function for my workmates.
The best thing would be to give your workmates a boost using boost.

Here you get well-documented, tested code. Much better than anything you can expect yourself to deliver.

I see this a lot, but I'll point out that many real projects don't
allow the programmers to drag in whatever third-party libraries they
feel like. It would take a long period of evaluation (as the Boost
stuff is non-standard and not provided by a vendor you can go to to
complain).

Brian

Jul 22 '05 #10

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

Similar topics

5
12241
by: Andy Mee | last post by:
Hello one and all, I'm developing an Asp.NET system to take a CSV file uploaded via the web, parse it, and insert the values into an SQL database. My sticking point comes when I try to split() the string returned by readline() on the file. The following code snippet works for me: tokens = "one,two,three,four".Split(",") for each token in tokens response.write("<td>"+token+"</td>")
4
649
by: indranil b | last post by:
void tokenise(const string& data, const string& separator, vector<string>& fields) { size_t n = data.length(); size_t start = data.find_first_not_of(separator); while (start < n) { size_t stop = data.find_first_of(separator, start); if (stop > n) stop = n; fields.push_back(data.substr(start, stop-start)); start = data.find_first_not_of(separator, stop+1);
5
57205
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I could get only 1st line, but it is not returning 2nd line. here's code: string address = null; string ssep = Environment.NewLine; char sep = ssep.ToCharArray();
4
3387
by: dchan1 | last post by:
String line = " I am new to c# "; How to use Regex to split the string so that It would return a String array with: token = "I" token = "am" token = "new" token = "to" token = "c#"
22
2938
by: Sven-Thorsten Fahrbach | last post by:
Hi Does anybody know of a library that offers a function to split pathnames. It should work somewhat like the following code snippet: ----------------- char *path = "/home/user/Documents/Textdocuments/Bills"; char **dirs; splitPath(path, dirs);
15
6005
by: nagar | last post by:
I need to split a string whenever a separator string is present (lets sey #Key(val) where val is a variable) and rejoin it in the proper order after doing some processing. Is there a way to use the Regex.Split function to split the string whenever the #Key(val) occurrs but that keeps the #Key(val) occurrences to that I can reconstruct the final string after doing certain operations on each token (I need to basically convert each string...
10
2516
by: teddyber | last post by:
Hello, first i'm a newbie to python (but i searched the Internet i swear). i'm looking for some way to split up a string into a list of pairs 'key=value'. This code should be able to handle this particular example string : qop="auth,auth-int,auth-conf",cipher="rc4-40,rc4-56,rc4,des, 3des",maxbuf=1024,charset=utf-8,algorithm=md5-sess
6
1867
by: giffy | last post by:
HOW to get the spaces out of a delimited string ?? here is a usage scenario where split() seems not to work String origAVNListStr = "~`L~`L~`L~`~`~`L~`"; String strArr = origAVNListStr.split("~`"); for(int i=0;i<strArr.length;i++) { System.out.println(i+1 +"the next token - " + strArr); } and the output is
5
1523
mickey0
by: mickey0 | last post by:
hello, I have to read a text and I decided to split it with Scanner class. private Vector<String> splitWords(Scanner s) { Vector<String> v = new Vector<String>(); while ( s.hasNext() ) { String token = s.next(); if ( token.length() == 1 && (token == "." || token == ";" || token == "," || token == ":") ) { continue;
0
9685
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
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10465
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
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...
0
6800
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
5453
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.