Y0UR
Y0UR

Reputation: 31

C++ std::string search with special characters

I got a long string from a HTML request. I need to find the following substring within:

<b><i>

However when I'm trying to search it gives me wrong results.

If I replace the string with something, not containing < or > it works flawlessly.

Relevant code: (not copied, might have some typos, but works without special characters)

std::string readBuffer; //longlongstring
std::string starttag;
std::string endtag;

size_t sstart;
size_t send;

//...................

sstart=readBuffer.find(starttag);
send=redBuffer.find(endtag);

correction=readBuffer.substr(sstart,send-sstart);

//....................

So yeah, if anyone happens to know a way to fix this, I'd very much be greatful :) Thanks in advance

Upvotes: 0

Views: 874

Answers (1)

Vaughn Cato
Vaughn Cato

Reputation: 64308

This works as expected:

#include <cassert>
#include <string> 

int main(int,char**)
{
  std::string data = "...<b><i>Berlin</b></i>";
  size_t sstart = data.find("<b><i>")+6;
  size_t send = data.find("</b></i>");
  std::string correction = data.substr(sstart,send-sstart);
  assert(correction=="Berlin");
  return 0;
}

If you create a small complete example like this, but where you get a failure, then it would make it much easier to determine what the problem is.

Upvotes: 3

Related Questions