本文介绍了在STD :: string中搜索包含特殊字符的字符串(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Heya



我从HTML请求中获得了一个长字符串。我需要找到以下子串:



<& randomtag>



但是当我我试图搜索它给我错误的结果。



如果我用一些东西替换字符串,不包含< > 中它完美无瑕。



相关代码:

(未复制,可能有一些拼写错误,但没有特殊字符)



Heya

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

<&randomtag>

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

If I replace the string with something, not containing "<" ">" 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);

std::cout << correction << std::endl;





所以是的,如果有人碰巧知道解决这个问题的方法,我会非常感激:)

提前致谢



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

推荐答案

std::string readBuffer = "dgsdg sdfgsd sdfg ds <&randomtag> fghhsdfgsd dfgd sdg dfgdg dfg  ghdfh sdfg";
std::string starttag = "<&randomtag>";
std::string endtag = "";

size_t sstart;
size_t send;

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

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

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





和它按预期工作。更正变量持有<& randomtag> fghhsdfgsd dfgd sdg dfgdg dfg。



and it worked as expected. correction variable was holding "<&randomtag> fghhsdfgsd dfgd sdg dfgdg dfg ".


这篇关于在STD :: string中搜索包含特殊字符的字符串(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:45