c 截取字符串的方法是什么(截取字符串的方法介绍)

C find()函数用法举个例子:#include <iostream> // std::cout#include <algorithm> // std::find#include <vector> // std::vectorusing namespace std;int main() { //find() 函数作用于普通数组 char stl[] =”http://c.biancheng.net/stl/”; //调用 find() 查找第一个字符 ‘c’ char * p = find(stl, stl strlen(stl), ‘c’); //判断是否查找成功 if (p != stl strlen(stl)) { cout << p << endl; } //find() 函数作用于容器 std::vector<int> myvector{ 10,20,30,40,50 }; std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) cout << “查找成功:” << *it; else cout << “查找失败”; return 0;}
c  截取字符串的方法是什么(截取字符串的方法介绍)

对于 find() 函数的底层实现,C 标准库中给出了参数代码,感兴趣的读者可自行研究:

template<class InputIterator, class T>InputIterator find (InputIterator first, InputIterator last, const T& val){ while (first!=last) { if (*first==val) return first; first; } return last;}

查找第一次出现的目标字符串:

#include<iostream>#include<cstdio>usingnamespacestd;int main(){strings1=”abcdef”;strings2=”de”;intans=s1.find(s2);//在S1中查找子串S2cout<<ans<<endl;system(“pause”);}

说明:如果查找成功则输出查找到的第一个位置,否则返回-1;

查找从指定位置开始的第一次出现的目标字符串:

#include<iostream>#include<csdtio>using namespace std;int main(){strings1=”abcdef”;strings2=”de”;intans=s1.find(s2,2);//从S1的第二个字符开始查找子串S2cout<<ans<<endl;system(“pause”);}

find_first_of()表示查找字符串的某个字符最先出现的位置,find_first_of()不是全匹配,即它不是必须要查找的字符串在被查找的字符串中全部出现,而是出现个别字符即可。(我觉得这是个神马需求?没用过!!!)

find_last_of()函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。

rfind()函数是反向查找字符串,即找到最后一个与子串匹配的位置。

find_first_not_of()函数是找到第一个不与子串匹配的位置。(这个也没有用到过)

2、C substr()

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

语法:

substr(size_type _Off = 0,size_type _Count = npos)一种构造string的方法

形式 :s.substr(pos, len)

返回值:string,包含s中从pos开始的len个字符的拷贝(pos的默认值是0,len的默认值是s.size() – pos,即不加参数会默认拷贝整个s)

异常 :若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾

#include<iostream>#include<string>using namespace std;int main(){ string s=”sfsa”; string a=s.substr(0,3); string b=s.substr(); string c=s.substr(2,3); cout<<a<<endl; cout<<b<<endl; cout<<c<<endl; return 0;}

3、C replace()(这变态,重载了9个,给我挑的上气不接下气,不管他重载多少我们快速找出最适合自己那一款就好)

用法一:用str替换指定字符串从起始位置pos开始长度为len的字符

用法二:用str替换迭代器起始位置和结束位置的字符

用法三:用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串

用法四:string转char*时编译器可能会报出警告,不建议这样做

用法五:string转char*时编译器可能会报出警告,不建议这样做

用法六:string转char*时编译器可能会报出警告,不建议这样做

用法七:string转char*时编译器可能会报出警告,不建议这样做

用法八:用重复n次的c字符替换从指定位置pos长度为len的内容

用法九:用重复n次的c字符替换从指定迭代器位置(从i1开始到结束)的内容

这九个介绍往这里一列给人看的脑瓜子嗡嗡的!

上例子!!!(这边我写3个,其他的都是大同小异的东西,一通百通;用的时候稍微Baidu一下瞬间会了,平时经常不用也记不住)

用法一:用str替换指定字符串从起始位置pos开始长度为len的字符

string&replace(size_tpos,size_tlen,conststring&str);

#include<iostream>#include<string>using namespace std;int main(){string str = “he is@ a@ good boy”;str=str.replace(str.find(“a”),2,”#”); //从第一个a位置开始的两个字符替换成#cout<<str<<endl; return 0;}

用法二:用str替换迭代器起始位置和结束位置的字符string&replace(const_iteratori1,const_iteratori2,conststring&str);

#include<iostream>#include<string>using namespace std;int main(){string str = “he is@ a@ good boy”; str=str.replace(str.begin(),str.begin() 5,”#”); //用#替换从begin位置开始的5个字符 cout<<str<<endl; return 0; }

用法三:用substr的指定子串(给定起始位置和长度)替换从指定位置上的字符串

string&replace(size_tpos,size_tlen,conststring&str,size_tsubpos,size_tsublen);

#include<iostream>#include<string>using namespace std;int main(){string str = “he is@ a@ good boy”; str=str.replace(str.begin(),str.begin() 5,”#”); //用#替换从begin位置开始的5个字符 cout<<str<<endl; return 0; }

4、C 中string::npos的一些用法总结

npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。

#include <iostream> #include <limits> #include <string> using namespace std; int main() { size_t npos = -1; cout << “npos: ” << npos << endl; cout << “size_t max: ” << numeric_limits<size_t>::max() << endl;}

执行结果为:

npos: 4294967295

size_t max: 4294967295

可见他们是相等的,也就是说npos表示size_t的最大值

npos可以表示string的结束位置,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值的情况下会返回string::npos。举例如下(计算字符串中含有的不同字符的个数):

#include <iostream>#include <string>using namespace std;int main(){ string b; getline(cin,b); int count=0; for(int i=0;i<=127;i ) if(b.find(i)!=string::npos) count ; cout<<count;}

string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:

tmpname.replace(idx 1, string::npos, suffix);

这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx 1表示,string的剩余部分。

#include <iostream> #include <limits> #include <string> using namespace std; int main() { string filename = “test.cpp”; cout << “filename : ” << filename << endl; size_t idx = filename.find(‘.’); //as a return value if(idx == string::npos) { cout << “filename does not contain any period!” << endl; } else { string tmpname = filename; tmpname.replace(idx 1, string::npos, “xxx”); //string::npos作为长度参数,表示直到字符串结束 cout << “repalce: ” << tmpname << endl; } }

执行结果如下:

filename:test.cpp

replace: test.xxx

值得注意的地方:

int idx = str.find(“abc”);if (idx == string::npos) …

上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。

要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find(“abc”) == string::npos) { … }

写在最后的一句话:

万丈高楼平地起,再普通的改变也能改变普通。

发表评论

登录后才能评论