何问起C++教程 - 引言 - C++语言基础
c++中获取string长度和查找子字符串
可以使用string的length()方法和find()方法。
length()方法返回字符串的长度,find()方法返回子字符串在原字符串中的位置。
find方法如果返回-1表示没找到。返回0或正整数,表示有找到,且返回的是子字符串在原字符串中的起始位置。
示例代码如下:

#include <string>
#include<iostream>
using namespace std;
int main()
{
string m_hovertree = "hellosss";
int m_length = m_hovertree.length();
int m_positon = m_hovertree.find("he");

cout <<"position:"<< m_positon << endl;

cout << "length:" << m_length << endl;

return 0;
}
执行结果:
position:0
length:8
收藏 列表

评论: