Jump to content
  • Advertisement
Sign in to follow this  
xtothat

[SOLVED] [C++] Erase.Substring concatenation problem.

This topic is 3481 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

Hi! Just a quick question... Probably got a totally n00by answer... I'm doing some text parsing, and using the following code:
if(tmpIWLIST_enckey == "on")
{
  if(tmpIWLIST_enckey.substr(0, 50).find("IE") != string::npos)
  {
    string tmpIWLIST_enctype = tmpIWLIST.erase(0, tmpIWLIST.find("IE")+4);
    string tmpIWLIST_enctype2 = tmpIWLIST_enctype.substr(0, tmpIWLIST.find("\n"));
    cout << i << ":ENCTYPE:" << tmpIWLIST_enctype2 << endl;
  }
  else
  {
    cout << i << ":ENCTYPE:WEP" << endl;
  }
}
The problem I have, you may even be able to see, is that I'm having to use two strings to get what I want. If I add .substr(0, tmpIWLIST.find("\n")) to the end of the first string, I end up with a string which is only two characters long. Any ideas? Don't like pointless code. Thanks anyway in advance!!! X-T [Edited by - xtothat on March 13, 2009 10:36:28 AM]

Share this post


Link to post
Share on other sites
Advertisement

if(tmpIWLIST_enckey == "on")
{
if(tmpIWLIST_enckey.substr(0, 50).find("IE") != string::npos)


I don't get how you can expect to find "IE" from the string if you just established that it equals "on".

Another thing is that you can store the found position so you won't need to repeat the search:



size_t pos = string.find("x");
if (pos != string::npos) {
//use pos
}


(In case of loops, you can also perform the assignment within the loop condition - one of the few places where I'd do that.)

Share this post


Link to post
Share on other sites
The erase() function is destructive so assigning the result to a new string is redundant so you can change:

string tmpIWLIST_enctype = tmpIWLIST.erase(0, tmpIWLIST.find("IE")+4);
string tmpIWLIST_enctype2 = tmpIWLIST_enctype.substr(0, tmpIWLIST.find("\n"));


to:

tmpIWLIST.erase(0, tmpIWLIST.find("IE")+4);
string tmpIWLIST_enctype2 = tmpIWLIST.substr(0, tmpIWLIST.find("\n"));

Share this post


Link to post
Share on other sites
Hi, and thanks for all your input.

Sorry made a typo... It was supposed to read:

if(tmpIWLIST_enckey == "on")
{
if(tmpIWLIST.substr(0,50).find("IE") != string::npos


That's good enough for me! Looks a lot nicer now I've reduced it to just the one variable

Thanks!!!

X-T

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!