468,512 Members | 1,477 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

split, splice and the like

Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def

or I have to write something up?
thanks
Aug 3 '06 #1
7 6001
Gary Wessle wrote:
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def
Google Groups [ perl split group:comp.lang.c++ ]

There are always good versions available. Not as good as boost::regex or the
new draft-Standard or whatever regex, but still good.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Aug 3 '06 #2
Gary Wessle wrote:
Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def

or I have to write something up?
thanks
string::substr

or, look at the generalized copy ctor which takes range paramters

Aug 3 '06 #3
Gary Wessle wrote:
Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def

or I have to write something up?
thanks
It's called substr(). It takes the position into the
string and a number of characters to copy.

string s1 = s.substr(0, 3);
substr s2 = s.substr(3, 3);
Aug 3 '06 #4
Phlip wrote:
Gary Wessle wrote:
>is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def

Google Groups [ perl split group:comp.lang.c++ ]

There are always good versions available. Not as good as boost::regex or the
new draft-Standard or whatever regex, but still good.
No need to go farther than the substr() method of string in the
existing standard.
Aug 3 '06 #5
Mark P <us****@fall2005REMOVE.fastmailCAPS.fmwrites:
Gary Wessle wrote:
Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def
or I have to write something up?
thanks

string::substr

or, look at the generalized copy ctor which takes range paramters
not sure why it will only work if I uncomment the "using namespace
std;"
but will not work this way below

#include <iostream>
#include <string>
using std::substr;
using std::cout;

//using namespace std;

int main() {

string s = "abcdef";

cout << s.substr(0,3) << '\n';
}

******** error ****************
g++ -c -o try.o try.cpp
try.cpp:3: error: 'std::substr' has not been declared
try.cpp: In function 'int main()':
try.cpp:10: error: 'string' was not declared in this scope
try.cpp:10: error: expected `;' before 's'
try.cpp:12: error: 's' was not declared in this scope
make: *** [try.o] Error 1
abc
Aug 3 '06 #6
Gary Wessle <ph****@yahoo.comwrites:
Mark P <us****@fall2005REMOVE.fastmailCAPS.fmwrites:
Gary Wessle wrote:
Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def
or I have to write something up?
thanks
string::substr

or, look at the generalized copy ctor which takes range paramters

not sure why it will only work if I uncomment the "using namespace
std;"
but will not work this way below

#include <iostream>
#include <string>
using std::substr;
using std::cout;

//using namespace std;

int main() {

string s = "abcdef";

cout << s.substr(0,3) << '\n';
}

******** error ****************
g++ -c -o try.o try.cpp
try.cpp:3: error: 'std::substr' has not been declared
try.cpp: In function 'int main()':
try.cpp:10: error: 'string' was not declared in this scope
try.cpp:10: error: expected `;' before 's'
try.cpp:12: error: 's' was not declared in this scope
make: *** [try.o] Error 1
abc
actually I added
using std::string;
to the top of the file which reduced the error to

****************
g++ -c -o try.o try.cpp
try.cpp:4: error: 'std::substr' has not been declared
make: *** [try.o] Error 1
abc
Aug 3 '06 #7
Gary Wessle wrote:
Gary Wessle <ph****@yahoo.comwrites:
>Mark P <us****@fall2005REMOVE.fastmailCAPS.fmwrites:
>>Gary Wessle wrote:
Hi
is there a method "split or something" to do this
string s = "abcdef"
string s1 = split(s,0,2) // gives abc
string s2 = split(s,3,5) // gives def
or I have to write something up?
thanks
string::substr

or, look at the generalized copy ctor which takes range paramters
not sure why it will only work if I uncomment the "using namespace
std;"
but will not work this way below

#include <iostream>
#include <string>
using std::substr;
using std::cout;

//using namespace std;

int main() {

string s = "abcdef";

cout << s.substr(0,3) << '\n';
}

******** error ****************
g++ -c -o try.o try.cpp
try.cpp:3: error: 'std::substr' has not been declared
try.cpp: In function 'int main()':
try.cpp:10: error: 'string' was not declared in this scope
try.cpp:10: error: expected `;' before 's'
try.cpp:12: error: 's' was not declared in this scope
make: *** [try.o] Error 1
abc

actually I added
using std::string;
to the top of the file which reduced the error to

****************
g++ -c -o try.o try.cpp
try.cpp:4: error: 'std::substr' has not been declared
make: *** [try.o] Error 1
abc
There's no such thing as std::substr. substr is a member function of
std::string and does not itself need any namespace qualification. Also
if you have "using std::string;" and "using std::cout;" you don't need
the more general "using namespace std;".
Aug 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Martin Lucas-Smith | last post: by
4 posts views Thread by Hans Almåsbakk | last post: by
4 posts views Thread by Steven D'Aprano | last post: by
7 posts views Thread by Jordi Rico | last post: by
8 posts views Thread by Flyzone | last post: by
3 posts views Thread by Patrick | last post: by
4 posts views Thread by slinky | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.