c ++ in substr () / find_last_of () applications

Background Code:

std :: string topic = topic_name.substr (topic_name.find_last_of ( '/') + 1); // topic_name no '/', the length

Related to:

1、find_last_of();

Analysis: From the end of the string to start looking substring or character, if found return to the index of the first character string (in fact, the first few characters of), if no match is returned std :: string :: npos (actually -1 )

 
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const noexcept;

 

 



Disclaimer: This article is the original article CSDN bloggers "NearXDU", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/zhangxiao93/article/details/54381613
 

2、substr();

0. Uses: a construction method of the string

1. The two parameters: s.substr (pos, n) --- "as used string str_tmp = s.substr (pos,);

Popular explained: In the beginning, taken s n characters from the character str_tmp in pos;

2. a parameter: s.substr (pos) ---- "Use as: string str_tmp = s.substr (pos);

Popular explained: s in the next character taken from the start pos str_tmp to the character;

3. Supplement: if the value exceeds the pos string size, the substr function throws an exception out_of_range; pos + if the value n exceeds the size of the string, the value of n substr adjusts only copied to the end of the string

Extended: find (), find_first_of ()

Guess you like

Origin www.cnblogs.com/woodyzhu/p/11845580.html