C++ string error with “find_first_of” function
Clash Royale CLAN TAG#URR8PPP
C++ string error with “find_first_of” function
I want to get the word which is between two quotes so im using std::string::find_first_of
but pos2 and pos1 are same values , it causes error.
std::string ss="<tag2 name = "Name1">";
std::size_t pos1=ss.find_first_of(""");
std::size_t pos2=ss.find_first_of(""",pos1);//i'am using pos1 as a base position to get pos2.
std::cout<<pos1<<std::endl<<pos2;
It doesn't cause an error for me. Here's a tip, so many people post to this site saying 'it causes an error', 'it doesn't work' etc. Don't be like that, if you have an error say what the error is. Obvious really.
– john
12 mins ago
sorry about that , i am newbie . Im trying to get the word between two quote for example "Name1" . I got position of first quote-which is pos1- without any problem but I couldn't get position of second quote -which is pos2- . pos2 and pos1 always come up with same value.I'm using pos1 as a base position to get pos2 but like i said pos1 and pos2 always come up with same value.
– Ekrem Köse
7 mins ago
@EkremKöse OK well see the answers below.
– john
4 mins ago
3 Answers
3
You misunderstand the second parameter to find_last_of
. It is the last character to be searched. So you just end up finding the same character again. Remove the second parameter and your code will work
find_last_of
std::string ss="<tag2 name = "Name1">";
std::size_t pos1=ss.find_first_of(""");
std::size_t pos2=ss.find_last_of(""");
std::cout<<pos1<<std::endl<<pos2<<std::endl;
actually i dont want to use find_last_of , because when there is a string like that <b value = "BadVal" size = "10"> i couldn't get value of size , if i use find_last of to get size , it will be like BadVal" size="10;
– Ekrem Köse
3 mins ago
According to reference
, second parameter is not position to start searching from, but position to end on.
pos -
Position of the last character in the string to be considered in the search.
Any value greater than, or equal to, the string length (including string::npos) means that the entire string is searched.
Note: The first character is denoted by a value of 0 (not 1).
In your case, you can simply drop second argument and you'll recieve correct position in pos2
pos2
To find the next occurence of " after position pos1, you can use ss.find_first_of('"',pos1+1);
ss.find_first_of('"',pos1+1);
ss.find_last_of('"',pos1);
will look for a quote starting at the first quote you found but looking backwards/towards the beginning of the string. You can use ss.find_last_of('"');
to find the last occurence of a quote in the whole string.
ss.find_last_of('"',pos1);
ss.find_last_of('"');
In your example, ss.find_first_of('"',pos1+1);
and ss.find_last_of('"');
will yield the same result because the string contains two quotes and therefore, the second quote is also the last quote.
ss.find_first_of('"',pos1+1);
ss.find_last_of('"');
He doesn't want to find the next occurance, he wants the last occurance
– john
8 mins ago
By my interpretation of "the word which is between two quotes" cannot contain a quote.
– Pelipap
7 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"[I]t causes error" is unfortunately a pretty bad problem description. What error? How? What did you expect, and what did you get? Please read about how to ask good questions and this question checklist. Lastly please learn how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
14 mins ago