substr(C++语言函数)

substr(C++语言函数)

basic_string substr(size_type _Off = 0,size_type _Count = npos) const;

substr是C++语言函数,主要功能是复制子字符串,要求从指定位置开始,并具有指定的长度。如果没有指定长度_Count或_Count+_Off超出了源字符串的长度,则子字符串将延续到源字符串的结尾。

定义和用法

basic_string::substr

basic_string substr( size_type pos = 0,
                     size_type count = npos ) const;

   

参数

pos

所需的子字符串的起始位置。字符串中第一个字符的索引为 0,默认值为0.

count

子串的长度

返回值

一个子字符串,从其指定的位置开始

示例

Code : C++中 的代码如下

#include

#include

using namespace std;

int main()

{

	string str1("Hello");

	cout << " str1 is:" << str1<str2 = str1.substr(2, 3);

	cout << "The substring str1 copied is:" << str2 << endl;

	basic_stringstr3 = str1.substr();

	cout << "The default substring str3 is:" << endl;

	cout << str3 << endl;

	system("pause");
	return 0;

}

 

 

输出结果:

substr(C++语言函数)_第1张图片

 

你可能感兴趣的:(substr(C++语言函数))