如何获得C ++中的一个字符串的一部分? [英] How do I get a part of the a string in C++?
本文介绍了如何获得C ++中的一个字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C ++中获取字符串的一部分?我想知道从0到i的元素。
How do I get a part of a string in C++? I want to know what are the elements from 0 to i.
推荐答案
您想使用 std: :string :: substr
。这里有一个例子,无耻地从 http://www.cplusplus.com/reference/string/string/substr/
You want to use std::string::substr
. Here's an example, shamelessly copied from http://www.cplusplus.com/reference/string/string/substr/
// string::substr
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="We think in generalities, but we live in details.";
// quoting Alfred N. Whitehead
string str2, str3;
size_t pos;
str2 = str.substr (12,12); // "generalities"
pos = str.find("live"); // position of "live" in str
str3 = str.substr (pos); // get from "live" to the end
cout << str2 << ' ' << str3 << endl;
return 0;
}
这篇关于如何获得C ++中的一个字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文