string类find
时间: 2023-11-25 15:37:41 浏览: 31
string类中的find函数是用来查找子字符串在原字符串中的位置的。它的语法形式为:
```cpp
size_t find(const string& str, size_t pos = 0) const;
```
其中,`str` 是要查找的子字符串,`pos` 是指定搜索的起始位置,默认为0。
这个函数会返回子字符串在原字符串中首次出现的位置,如果找不到则返回`string::npos`。以下是一个示例用法:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string subStr = "world";
size_t found = str.find(subStr);
if (found != std::string::npos) {
std::cout << "Substring found at position: " << found << std::endl;
} else {
std::cout << "Substring not found." << std::endl;
}
return 0;
}
```
输出结果为:Substring found at position: 7
相关问题
string类find()
string类的find()方法是用于在字符串中查找指定子字符串的位置。它返回子字符串第一次出现的位置,如果没有找到则返回string::npos。该方法有多个重载函数,可以指定查找的起始位置和查找的次数等参数。此外,string库还提供了其他相关的查找方法,如rfind()、find_first_of()、find_last_of()、find_first_not_of()、find_last_not_of()等。这些方法的重载函数特征标都与find()方法相同。
string.find
string.find 是 Lua 编程语言中的一个函数,用于在字符串中查找指定的子字符串,并返回其在字符串中的位置。它的语法如下:
```
string.find(s, pattern, [init, [plain]])
```
其中,s 是要查找的字符串,pattern 是要查找的子字符串或正则表达式,init 是可选参数,表示开始查找的位置,默认为 1,plain 是可选参数,表示是否关闭正则表达式匹配,默认为 false。
该函数返回两个值,第一个值是找到的子字符串在字符串中的起始位置,如果没找到则返回 nil,第二个值是找到的子字符串在字符串中的结束位置,如果没找到则返回 nil。例如:
```
local s = "hello world"
local pos1, pos2 = string.find(s, "world")
print(pos1, pos2) -- 7 11
```
上述代码中,string.find 函数查找字符串 s 中的子字符串 "world",并返回其在 s 中的起始位置 7 和结束位置 11。