谁能给我讲解一下string的substring和find的用法,要有举例

来源:百度知道 编辑:UC知道 时间:2024/06/18 04:23:02
我知道msdn上有,不过我刚接触这方面的东西,并不能完全理解msdn所讲的内容

#include <iostream>
#include <String>

using namespace std;

void main()
{
string str = "Hello World";

int a = str.find("W"); //find的作用是返回参数在字符串中的位置
cout<<a<<endl; //输出结果6

string sub= str.substr(0,5); //substr的作用是截取字符串,第1个参数为起始位置,第2个参数为截取长度
cout<<sub<<endl; //输出结果Hello
}

查看help或msdn