split, splice and the like

G

Gary Wessle

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
 
P

Phlip

Gary said:
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.
 
M

Mark P

Gary said:
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
 
R

Ron Natalie

Gary said:
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);
 
R

Ron Natalie

Phlip said:
Gary said:
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.
 
G

Gary Wessle

Mark P said:
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
 
G

Gary Wessle

Gary Wessle said:
Mark P said:
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
 
M

Mark P

Gary said:
Gary Wessle said:
Mark P said:
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;".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top