473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(string 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 29787
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(string 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**********@Home.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.net...
| Hello everyone! :-D
| OK, I've came across many functions for this, but none works! I need
one
| that works like this:
| StringSplit(string 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(string 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::istringstream is(s);
return std::vector(std::istream_iterator<std::string>(is) ,
std::istream_iterator<std::string>());
}
Oct 15 '05 #5
thx!
"red floyd" <no*****@here.dude> wrote in message
news:SE*****************@newssvr29.news.prodigy.ne t...
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(string 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::istringstream is(s);
return std::vector(std::istream_iterator<std::string>(is) ,
std::istream_iterator<std::string>());
}

Oct 15 '05 #6
thx!
"Peter_Julian" <pj@antispam.codigo.ca> wrote in message
news:xS********************@news20.bellglobal.com. ..

<ch***@email.ua> wrote in message news:di**********@news.lucky.net...
| Hello everyone! :-D
| OK, I've came across many functions for this, but none works! I need
one
| that works like this:
| StringSplit(string 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*****@bigfoot.com> wrote in message
news:f4********************************@4ax.com...
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(string 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**********@Home.com

Oct 15 '05 #8
thx!
<An**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.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<typename CharType>
class SplitCharTraits
{
public:
static size_t __cdecl length(const CharType* s)
{
return std::char_traits<CharType>::length(s);
}
static char* strstr(const char* string, const char* strSearch)
{
return std::strstr(string, strSearch);
}
static wchar_t* strstr(const wchar_t* string, const wchar_t*
strSearch)
{
return std::wcsstr(string, strSearch);
}
};

template<typename 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<CharType*>(str);
CharType* r = NULL;
r = SplitCharTraits<CharType>::strstr(pstr, delim);
int dlen = SplitCharTraits<CharType>::length(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>::length(cp) > 0 || empties )
results.push_back(StrType(cp));
delete[] cp;
pstr = r + dlen;
r = SplitCharTraits<CharType>::strstr(pstr, delim);
}
if( SplitCharTraits<CharType>::length(pstr) > 0 || empties )
results.push_back(StrType(pstr));
return results.size();
}

Oct 15 '05 #10

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

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


Look at Splitting string into vector of vectors at
* http://groups.google.com/group/sourc...993fb8841382c8
* http://groups.google.com/group/log-f...5be21f949acdcc

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
Oct 25 '05 #11
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(string str, string delim, vector<string> results)

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


Here is one I wrote for my app, but it only splits on tabs (and
newlines):
// setcol() sets the elements of the colv vector
// to the individual phrases found in input_record, which is tab-delimited.
//
// The column numbers start at 0.
//
// The number of columns found is returned.
int
setcol(const std::string& input_record, std::vector<std::string>& colv)
{
std::istringstream s(input_record);
std::string temp;

colv.clear();
while (std::getline(s, temp, '\t')) {
colv.push_back(temp);
}

return static_cast<int>(colv.size());
}
--
Marcus Kwok
Oct 25 '05 #12

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

Similar topics

5
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...
9
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...
3
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....
2
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...
7
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...
5
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...
1
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...
4
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
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...

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.