Jump to content
  • Advertisement
Sign in to follow this  
lordoftools

extracting a string from a string

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

#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;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!