extracting a string from a string

Started by
6 comments, last by lordoftools 18 years, 8 months ago
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.
-Lordoftools"Knowledge is not only a gift it's a responcibility."
Advertisement
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

Proud to be a PAKISTANI.
strtok modifies the string you pass to it (it inserts null characters between the tokens) so using it like that would be illegal.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I present to you..

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.
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
#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; }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thank you much guys substr will work out great for my purpuses.

-Lordoftools
-Lordoftools"Knowledge is not only a gift it's a responcibility."

This topic is closed to new replies.

Advertisement