c++ string字符串操作
时间: 2023-08-18 07:08:25 浏览: 167
在C++中,可以使用C string的一些函数来对字符串进行操作。你可以使用`length()`函数来获取字符串的长度,然后可以使用循环和下标访问来获取字符串中的字符。例如,可以使用以下代码来获取字符串中的奇数位置的字符:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main () {
string s = "hello world!";
for (int i = 0; i < s.length() ; i++) {
if (i % 2) {
cout << s[i];
}
}
cout << endl;
return 0;
}
```
这段代码会输出字符串中奇数位置的字符,即"el ol"。。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++ string字符串分割](https://blog.csdn.net/qq_35308053/article/details/113643398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [c++ string字符串详解](https://blog.csdn.net/m0_62870588/article/details/123716198)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [【C++】字符串(string)的使用](https://blog.csdn.net/AAADiao/article/details/130755661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文