C++ std::string::find_last_of()函数(在字符串中搜索与参数中指定的任何字符匹配的最后一个字符)(从后往前找)(文件路径中找文件名,/\兼容windows和linux)
Posted Dontla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ std::string::find_last_of()函数(在字符串中搜索与参数中指定的任何字符匹配的最后一个字符)(从后往前找)(文件路径中找文件名,/\兼容windows和linux)相关的知识,希望对你有一定的参考价值。
文章目录
cppman std::string::find_last_of
std::string::find_last_of(3) C++ Programmer's Manual std::string::find_last_of(3)
NAME
std::string::find_last_of - Find character in string from the end //从末尾查找字符串中的字符
TYPE
public member function
SYNOPSIS
#include <string>
C++98
+--------------+----------------------------------------------------------------------------------------------------------------------+
| string (1) | size_t find_last_of (const string& str, size_t pos = npos) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
|c-string (2) | size_t find_last_of (const char* s, size_t pos = npos) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
| buffer (3) | size_t find_last_of (const char* s, size_t pos, size_t n) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
|character (4) | size_t find_last_of (char c, size_t pos = npos) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
+--------------+----------------------------------------------------------------------------------------------------------------------+
| string (1) | size_t find_last_of (const string& str, size_t pos = npos) const noexcept; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
|c-string (2) | size_t find_last_of (const char* s, size_t pos = npos) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
| buffer (3) | size_t find_last_of (const char* s, size_t pos, size_t n) const; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
|character (4) | size_t find_last_of (char c, size_t pos = npos) const noexcept; |
+--------------+----------------------------------------------------------------------------------------------------------------------+
DESCRIPTION
Searches the string for the last character that matches any of the characters specified in its arguments.
When pos is specified, the search only includes characters at or before position pos, ignoring any possible occurrences after pos.
//在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。
//当指定了pos时,搜索仅包括pos处或之前的字符,忽略pos之后可能出现的任何字符。
PARAMETERS
str Another string with the characters to search for.
//另一个包含要搜索的字符的字符串。
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).
//要在搜索中考虑的字符串中最后一个字符的位置。
//任何大于或等于字符串长度的值(包括字符串::npos)都表示搜索整个字符串。
//注:第一个字符由值0(而不是1)表示。
s Pointer to an array of characters.
If argument n is specified (3), the first n characters in the array are searched for.
Otherwise (2), a null-terminated sequence is expected: the length of the sequence with the characters to match is determined by
the first occurrence of a null character.
//指向字符数组的指针。
//如果指定了参数n(3),将搜索数组中的前n个字符。
//否则(2),会出现一个以空结尾的序列:要匹配的字符序列的长度由第一个出现的空字符决定。
n Number of character values to search for.
//要搜索的字符值数。
c Individual character to be searched for.
size_t is an unsigned integral type (the same as member type string::size_type).
//要搜索的单个字符。
//size_t是无符号整数类型(与成员类型string::size_type相同)。
RETURN VALUE
The position of the last character that matches.
If no matches are found, the function returns string::npos.
size_t is an unsigned integral type (the same as member type string::size_type).
//匹配的最后一个字符的位置。
//如果找不到匹配项,函数将返回字符串::npos。
//size_t是无符号整数类型(与成员类型string::size_type相同)。
EXAMPLE
// string::find_last_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
void SplitFilename (const std::string& str)
std::cout << "Splitting: " << str << '\\n';
std::size_t found = str.find_last_of("/\\
std::cout << " path: " << str.substr(0,found) << '\\n';
std::cout << " file: " << str.substr(found+1) << '\\n';
int main ()
std::string str1 ("/usr/bin/man");
std::string str2 ("c:\\216
SplitFilename (str1);
SplitFilename (str2);
return 0;
Splitting: /usr/bin/man
path: /usr/bin
file: man
Splitting: c:\\windows\\winhelp.exe
path: c:\\windows
file: winhelp.exe
COMPLEXITY
Unspecified, but generally up to linear in the string length (or pos) times the number of characters to match (worst case).
ITERATOR VALIDITY
No changes.
DATA RACES
The object is accessed.
EXCEPTION SAFETY
If s does not point to an array long enough, it causes undefined behavior.
Otherwise, the function never throws exceptions (no-throw guarantee).
SEE ALSO
string::find(3)
Find content in string (public member function)
string::rfind(3)
Find last occurrence of content in string (public member function)
string::find_first_of(3)
Find character in string (public member function)
string::find_last_not_of(3)
Find non-matching character in string from the end (public member function)
string::replace(3)
Replace portion of string (public member function)
string::substr(3)
Generate substring (public member function)
REFERENCE
cplusplus.com, 2000-2015 - All rights reserved.
cplusplus.com 2022-05-13 std::string::find_last_of(3)
示例:文件路径中找文件名(兼容windows和linux),/\\\\
多个字符匹配(前面找不到就继续找下一个)
// string::find_last_of
#include <iostream> // std::cout
#include <string> // std::string
#include <cstddef> // std::size_t
void SplitFilename(const std::string& str)
std::cout << "Splitting: " << str << '\\n';
std::size_t found = str.find_last_of("/\\\\");
std::cout << " path: " << str.substr(0,found) << '\\n';
std::cout << " file: " << str.substr(found + 1) << '\\n';
int main()
std::string str1("/usr/bin/man.cpp");
std::string str2("c:\\\\216.c");
SplitFilename(str1);
SplitFilename(str2);
return 0;
运行结果:
Splitting: /usr/bin/man.cpp
path: /usr/bin
file: man.cpp
Splitting: c:\\216.c
path: c:
file: 216.c
以上是关于C++ std::string::find_last_of()函数(在字符串中搜索与参数中指定的任何字符匹配的最后一个字符)(从后往前找)(文件路径中找文件名,/\兼容windows和linux)的主要内容,如果未能解决你的问题,请参考以下文章
[C++]C++入门到入土篇 HelloWorld 解析 && C++入门