473,568 Members | 2,850 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

split string

Hello everyone! :-D
OK, I've came across many functions for this, but none works! I need one
that works like this:
StringSplit(str ing str, string delim, vector<string> results)

Either with strings or char arrays, doesn't matter... anyone know? Thanks in
advance!


Oct 15 '05 #1
11 29806
On Sat, 15 Oct 2005 03:40:11 +0300, <ch***@email.ua > wrote:
Hello everyone! :-D
OK, I've came across many functions for this, but none works!
Hmmm...must be fairly common stuff...wonder why none of them worked?
I need one that works like this:
StringSplit(st ring str, string delim, vector<string> results)

Either with strings or char arrays, doesn't matter... anyone know? Thanks in
advance!


Check out the Boost tokenizer library:

http://www.boost.org

--
Bob Hairgrove
No**********@Ho me.com
Oct 15 '05 #2
This seems pretty straightforward . There's a find method
for std::string. Put that in a loop, and copy the appropriate
substring(s) to the vector.

Oct 15 '05 #3

<ch***@email.ua > wrote in message news:di******** **@news.lucky.n et...
| Hello everyone! :-D
| OK, I've came across many functions for this, but none works! I need
one
| that works like this:
| StringSplit(str ing str, string delim, vector<string> results)
|

There is too little space in this news message to show you the myriad of
member functions a std::string has available to satisfy your request.

consult...
http://www.sgi.com/tech/stl/basic_string.html

Once you've chosen a strategy, post the code of your attempt.
Oct 15 '05 #4
ch***@email.ua wrote:
Hello everyone! :-D
OK, I've came across many functions for this, but none works! I need one
that works like this:
StringSplit(str ing str, string delim, vector<string> results)

Either with strings or char arrays, doesn't matter... anyone know? Thanks in
advance!


I can't claim credit for this -- I saw it on c.l.c++ a while ago, but
assuming standard whitespace delimiters (and working from memory):

#include <string>
#include <sstream>
#include <iterator>
#include <vector>
std::vector<std ::string> split(const std::string& s)
{
std::istringstr eam is(s);
return std::vector(std ::istream_itera tor<std::string >(is),
std::istream_it erator<std::str ing>());
}
Oct 15 '05 #5
thx!
"red floyd" <no*****@here.d ude> wrote in message
news:SE******** *********@newss vr29.news.prodi gy.net...
ch***@email.ua wrote:
Hello everyone! :-D
OK, I've came across many functions for this, but none works! I need one
that works like this:
StringSplit(str ing str, string delim, vector<string> results)

Either with strings or char arrays, doesn't matter... anyone know? Thanks
in advance!


I can't claim credit for this -- I saw it on c.l.c++ a while ago, but
assuming standard whitespace delimiters (and working from memory):

#include <string>
#include <sstream>
#include <iterator>
#include <vector>
std::vector<std ::string> split(const std::string& s)
{
std::istringstr eam is(s);
return std::vector(std ::istream_itera tor<std::string >(is),
std::istream_it erator<std::str ing>());
}

Oct 15 '05 #6
thx!
"Peter_Juli an" <pj@antispam.co digo.ca> wrote in message
news:xS******** ************@ne ws20.bellglobal .com...

<ch***@email.ua > wrote in message news:di******** **@news.lucky.n et...
| Hello everyone! :-D
| OK, I've came across many functions for this, but none works! I need
one
| that works like this:
| StringSplit(str ing str, string delim, vector<string> results)
|

There is too little space in this news message to show you the myriad of
member functions a std::string has available to satisfy your request.

consult...
http://www.sgi.com/tech/stl/basic_string.html

Once you've chosen a strategy, post the code of your attempt.

Oct 15 '05 #7
thx!
"Bob Hairgrove" <in*****@bigfoo t.com> wrote in message
news:f4******** *************** *********@4ax.c om...
On Sat, 15 Oct 2005 03:40:11 +0300, <ch***@email.ua > wrote:
Hello everyone! :-D
OK, I've came across many functions for this, but none works!


Hmmm...must be fairly common stuff...wonder why none of them worked?
I need one that works like this:
StringSplit(s tring str, string delim, vector<string> results)

Either with strings or char arrays, doesn't matter... anyone know? Thanks
in
advance!


Check out the Boost tokenizer library:

http://www.boost.org

--
Bob Hairgrove
No**********@Ho me.com

Oct 15 '05 #8
thx!
<An**********@g mail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
This seems pretty straightforward . There's a find method
for std::string. Put that in a loop, and copy the appropriate
substring(s) to the vector.

Oct 15 '05 #9
This one is based on some code i found at www.codeproject.com years
ago. I extended it for more flexibility. This version now supports wide
character strings and any STL container type and most string
classes(STL string, MFC/ATL CString, etc. The only requirement for the
string class is a constructor that accepts a C style string pointer).

You can use it like this:

#include "split.h"
....
const char* a = "aaa bbb cccc";
list<string> results; // or vector<string> results;
split(a, " ", results);

---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8----

// split.h
#pragma once

#include <cstring>

template<typena me CharType>
class SplitCharTraits
{
public:
static size_t __cdecl length(const CharType* s)
{
return std::char_trait s<CharType>::le ngth(s);
}
static char* strstr(const char* string, const char* strSearch)
{
return std::strstr(str ing, strSearch);
}
static wchar_t* strstr(const wchar_t* string, const wchar_t*
strSearch)
{
return std::wcsstr(str ing, strSearch);
}
};

template<typena me CharType, typename ContainerType>
int split(const CharType* str, const CharType* delim,
ContainerType& results, bool empties = true)
{
typedef typename ContainerType:: value_type StrType;

CharType* pstr = const_cast<Char Type*>(str);
CharType* r = NULL;
r = SplitCharTraits <CharType>::str str(pstr, delim);
int dlen = SplitCharTraits <CharType>::len gth(delim);
while( r != NULL ) {
CharType* cp = new CharType[(r-pstr)+1];
memcpy(cp, pstr, sizeof(CharType )*(r-pstr));
cp[(r-pstr)] = 0;
if( SplitCharTraits <CharType>::len gth(cp) > 0 || empties )
results.push_ba ck(StrType(cp)) ;
delete[] cp;
pstr = r + dlen;
r = SplitCharTraits <CharType>::str str(pstr, delim);
}
if( SplitCharTraits <CharType>::len gth(pstr) > 0 || empties )
results.push_ba ck(StrType(pstr ));
return results.size();
}

Oct 15 '05 #10

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

Similar topics

5
31164
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it will split myString up using the delimiter of 1 space so that
9
2481
by: Java and Swing | last post by:
Say I have a string which contains numbers separated by a comma... such as "0,1,2,3,4,5"...I want to split the string at the commas and return an array containing, 0,1...5. Suggestions? I've tried something like... int *Split(char *msg) { int len = 0; char *tmp; char *sub_string = NULL;
3
6378
by: edoardo.poeta | last post by:
I'm a dummy. I have a basic knowledge of javascript and I want to split a string, but I receive an error at line 15. Where my error in make the array? Why? Can someone help me to resolve? Thank's. The name of file is E:\delibere test\pdf\2002\2002-01-01-GC-000-Testo.pdf <HTML> <HEAD> <SCRIPT LANGUAGE=JAVASCRIPT>
2
13194
by: eddiefisher41 | last post by:
Hey Guys. If I have a string say "abcdefg" how do i split the string without delimiters. What i need is a list containing each of the chars: e.g. The string.split() method wont work as it requires a delimiter within the string to split on and returns . Just going to use it for some error checking and validation used in a wx.TextCtrl in...
7
3484
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th character. For example: if the main string S is S='abcde fghc ijkl mnop'
5
2525
by: shaiful | last post by:
Hi all I have a simple problem with string. I want to split string, such as: dim s as string dim k(3) as string s="aa1,bv1,cc1,dt1" i want to split the value in k, k(0)=aa1 k(1)=bv1 k(2)=cc1 k(3)=dt1
1
1588
by: Garima12 | last post by:
Hi, There is a string like: parcel = 1 AND id = 546 OR shape = 'point' I want to split this string with AND and OR I wrote split function like: var contentarr = qString2.split("AND"); it will split the string where the AND is but I want string to be broken:
4
10204
by: dmitrey | last post by:
hi all, howto split string with both comma and semicolon delimiters? i.e. (for example) get from string "a,b;c" I have tried s.split(',;') but it don't work Thx, D.
4
7728
by: N9 | last post by:
Hi Anyone who can help about split string. string text = "History about a boy, who loves to play baseball with his friends." I like to find indexOf "play" and read the string 10 char left and 10 char right
0
7604
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...
0
7916
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. ...
0
8117
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...
1
7660
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...
0
6275
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...
0
5217
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...
1
2101
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
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
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...

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.