简体   繁体   English

C ++ std :: string搜索带有特殊字符

[英]C++ std::string search with special characters

I got a long string from a HTML request. 我从HTML请求中得到了一个长字符串。 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 所以,是的,如果有人碰巧知道解决此问题的方法,我将非常感激:)预先感谢

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. 如果您创建了一个像这样的小型完整示例,但是却遇到了失败,那么它将更容易确定问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM