首页 C/C++修行正文

C++ 字符串截取系列

欲儿 C/C++修行 2019-08-01 647 0

从数位上截取

#include <iostream>
从位数上截取
#include <string> 
using namespace std; 
int main() 
{ 
string str = "123456"; 
string result = str.substr(0 , 1);//所以这里是从第0个开始截取,然后我们只截取长度为1,所以 result应该等于1, 如果输入的是str.substr(0 , 2)那就应该返回12 
cout << result << endl ; 
system("pause"); 
return 0; 
}

从两个字符串之间截取

#include <iostream> 
#include <string> 
using namespace std; 
int main() 
{ 
string str = "123这是一段中文截取456"; 
string result = str.substr(str.find_first_of("2") + 1, str.find_first_of("5") - str.find_first_of("2")  -1 ); //你可能无法理解,但是你前期不用管,将就用吧,其实find函数返回的值是该字符在多少位,所以截取本 身还是上一个函数的原理嗯 
cout << result << endl ; 
system("pause"); 
return 0; 
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论