c++ string find 不区分大小写
时间: 2023-07-21 08:08:09 浏览: 1563
在C++中,string类的find()函数默认是区分大小写的。如果你想要进行不区分大小写的查找,可以使用一些其他的方法来实现,比如将字符串转换为小写或大写后再进行查找。以下是一个通过转换为小写实现不区分大小写查找的例子:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::string str = "Hello World";
std::string search = "WORLD";
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
std::transform(search.begin(), search.end(), search.begin(), ::tolower);
size_t position = str.find(search);
if (position != std::string::npos) {
std::cout << "Found at position: " << position << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}
```
这个例子中,我们将字符串str和search都转换为小写形式,然后使用find()函数进行查找。这样就可以实现不区分大小写的字符串查找了。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)