string从字符串结尾查找
时间: 2023-11-21 21:06:00 浏览: 42
在`string`中,你可以使用`rfind()`成员函数从字符串结尾开始查找指定的子字符串,其语法如下:
```cpp
size_t rfind(const string& str, size_t pos = npos) const noexcept;
```
其中,`str`是要查找的子字符串,`pos`是要开始查找的位置。如果未指定`pos`,则默认从字符串末尾开始查找。如果找到了子字符串,则返回子字符串的索引;否则返回`string::npos`。
例如,以下代码演示了如何使用`rfind()`函数:
```cpp
string str = "hello world";
size_t index = str.rfind("world"); // 从字符串结尾查找"world"
if (index != string::npos) {
cout << "找到了,索引为:" << index << endl;
} else {
cout << "未找到" << endl;
}
```
输出结果为:
```
找到了,索引为:6
```
在这个例子中,`rfind()`函数从字符串`str`的结尾开始查找子字符串`"world"`,并返回其索引值6。
相关问题
java string类字符串查找实现
Java中的String类提供了多种方法来查找字符串,下面介绍几种常用的方法:
1. indexOf() 方法:返回指定子字符串在原字符串中第一次出现的位置。如果没有找到匹配的字符串,则返回-1。
```
String str = "Hello World";
int index = str.indexOf("World");
System.out.println(index); // 输出:6
```
2. lastIndexOf() 方法:返回指定子字符串在原字符串中最后一次出现的位置。如果没有找到匹配的字符串,则返回-1。
```
String str = "Hello World";
int index = str.lastIndexOf("l");
System.out.println(index); // 输出:9
```
3. contains() 方法:判断原字符串是否包含指定的子字符串。
```
String str = "Hello World";
boolean contains = str.contains("World");
System.out.println(contains); // 输出:true
```
4. matches() 方法:判断原字符串是否匹配指定的正则表达式。
```
String str = "Hello World";
boolean matches = str.matches("Hello.*");
System.out.println(matches); // 输出:true
```
5. startsWith() 方法:判断原字符串是否以指定的子字符串开头。
```
String str = "Hello World";
boolean startsWith = str.startsWith("Hello");
System.out.println(startsWith); // 输出:true
```
6. endsWith() 方法:判断原字符串是否以指定的子字符串结尾。
```
String str = "Hello World";
boolean endsWith = str.endsWith("World");
System.out.println(endsWith); // 输出:true
```
字符串函数-从字符串中查找子串
在Python中,可以使用以下函数从字符串中查找子串:
1. find()函数:该函数返回子串在字符串中第一次出现的位置,如果没有找到则返回-1。例如:
```
string = "Hello World"
sub_string = "World"
pos = string.find(sub_string)
print(pos) # 输出:6
```
2. index()函数:该函数与find()函数类似,但是如果子串不存在则会抛出异常。例如:
```
string = "Hello World"
sub_string = "World"
try:
pos = string.index(sub_string)
print(pos)
except ValueError:
print("子串不存在") # 输出:6
```
3. count()函数:该函数返回子串在字符串中出现的次数。例如:
```
string = "Hello World"
sub_string = "l"
count = string.count(sub_string)
print(count) # 输出:3
```
4. startswith()函数和endswith()函数:这两个函数分别用于判断字符串是否以指定的子串开头或结尾,返回布尔值。例如:
```
string = "Hello World"
sub_string1 = "He"
sub_string2 = "ld"
result1 = string.startswith(sub_string1)
result2 = string.endswith(sub_string2)
print(result1) # 输出:True
print(result2) # 输出:True
```
5. in关键字:这个关键字可以用于判断一个字符串是否包含另一个字符串,返回布尔值。例如:
```
string = "Hello World"
sub_string = "World"
result = sub_string in string
print(result) # 输出:True
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)