extracting a string from a string
grr computer decided to reboot just as i finished this post so im gonna make this one short.
got a std::string named s1 which == "name=blah".
i need s2 to == "blah" when i don't know what is after the '=' in s1.
I can use std::string::find to find the '=' so i know the start, i can use std::string::strlen to find the end of it but i can't find a function which will read between two marker variables like that. I either need the syntax to the function which can preform this task or the coding on how to go about it.
s1 starts off as a char [2000] pulled in with getline from a ifstream file then converted to a string so if you can find someway of making a char [2000] pull the end out and put it into a string that's fine but the end result must be a string.
Thanks you
-Lordoftools
P.S. sorry if im being straight to the point and blunt but im tired and already wrote one of these posts in detail and politly then my comp restarted. Goodnight.
Hello
From what you have written here, I assume that you want to split the string into two strings based on a seperator character. Try using strtok and pass it the char* like so
///////////////////////////////////////
string s="hello=blah";
char *ptr = s.c_str();
char* token = strtok(ptr,'=');
while(token!=NULL)
{
token = strtok(NULL,'=');
}
///////////////////////////////////////
Do refer to specs for th exact syntax
Thanx
Mobeen
From what you have written here, I assume that you want to split the string into two strings based on a seperator character. Try using strtok and pass it the char* like so
///////////////////////////////////////
string s="hello=blah";
char *ptr = s.c_str();
char* token = strtok(ptr,'=');
while(token!=NULL)
{
token = strtok(NULL,'=');
}
///////////////////////////////////////
Do refer to specs for th exact syntax
Thanx
Mobeen
strtok modifies the string you pass to it (it inserts null characters between the tokens) so using it like that would be illegal.
I present to you..
std::string::substr
Now with newer, fresher links provided :D
(void were prohibited)
Rigear
std::string::substr
Now with newer, fresher links provided :D
(void were prohibited)
Rigear
strtok() was always a terrible, terrible function. In C++ it deserves to go the way of the gotosaur.
Once you have the position of the = character from find(), use the string::substr() command to extract the characters that come after that.
Once you have the position of the = character from find(), use the string::substr() command to extract the characters that come after that.
If you are using std:string, you can use the find find_first_of, and substr methods. Something like:
std::string s1 = "name=blah", s2;
// find first "="
size_t index = s1.find_first_of("'=");
// make sure one was found
if(index != std::string::npos)
{
// starting at the next character, and going to the end of the string
s2 = s1.substr(index + 1);
}
-Scott
std::string s1 = "name=blah", s2;
// find first "="
size_t index = s1.find_first_of("'=");
// make sure one was found
if(index != std::string::npos)
{
// starting at the next character, and going to the end of the string
s2 = s1.substr(index + 1);
}
-Scott
#include <iostream>#include <string>int main() { std::string foo("true=false"); std::string::size_type token(foo.find('=')); if(token == std::string::npos) std::cout << "Holy bleep!"; else std::cout << "Left side: " << foo.substr(0, token) << '\n' << "Right side: " << foo.substr(token+1) << std::endl; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement