当前位置:   article > 正文

string查找和替换_xamarin string查找

xamarin string查找

1.用法描述:

  1. 查找:查找指定字符串是否存在
  2. 替换:在指定的位置替换字符串

2.函数原型:

  1. int find(const string &str,int pos=0) const; //从pos位置开始查找str第一次出现的位置
  2. int find(const char *s,int pos=0) const;//从pos位置开始查找s第一次出现位置
  3. int find(const char *s,int pos=0,int n) const; //从pos位置查找s的前n个字符第一次位置
  4. int find(const char c,int pos=0)  const; //从pos位置查找字符c的第一次出现位置
  5. int rfind(const string &str,int pos=npos) const; //从pos开始查找str最后一次位置,
  6. int rfind(const char *s,int pos=npos) const; //从pos开始查找s最后一次出现位置,
  7. int rfind(const char *s,int pos,int n) const; //从pos查找s的前n个字符最后一次位置
  8. int rfind(const char c,int pos=0) const;//查找字符c最后一次出现位置
  9. string &replace(int pos,int n,const string &str);//替换从pos开始的n个字符为str
  10. string &replace(int pos,int n,const char *s);//替换从pos开始的n个字符为字符串s
  11. rfind和find的区别:find默认从左往右查找,而rfind则代表从右往左查;find找到字符串后返回查找到的字符串的第一个字符的下标,找不到则返回-1;replace替换时要指定从哪儿个位置开始起,其后有多少个字符,替换成什么样的字符串。

3.补充:

  1. find_first_of()表示查找字符串的某个字符最先出现的位置,find_first_of()不是全匹配,即它不是必须要查找的字符串在被查找的字符串中全部出现,而是出现个别字符即可。该函数是将被查找的字符串和查找的字符串中第一个重复的字符的下标返回回来。
  2. find_last_of()函数与find_first_of()功能差不多,只不过find_first_of()是从字符串的前面往后面搜索,而find_last_of()是从字符串的后面往前面搜索。
  3. find_first_not_of()函数是找到第一个不与子串匹配的位置。
    1. #include<iostream>
    2. #include<string>
    3. using namespace std;
    4. void search_factor()
    5. {
    6. int pos;//pos的全称为position,即位置的意思。
    7. //1.查找字符串“wo”在字符串"hello world!hello world!"中第一次出现的位置
    8. string s1="hello world!hello world!";
    9. pos=s1.find("wo") ;
    10. if(pos==-1)
    11. cout<<"查找失败!"<<endl;
    12. else
    13. cout<<"找到字符位置,其位置pos = "<<pos<<endl;
    14. //2. 查找字符串“wo”在字符串"hello world!hello world!"中最后一次出现的位置
    15. pos=s1.rfind("wo");
    16. cout<<"找到字符位置,其位置pos = "<<pos<<endl;
    17. //3.查找字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置
    18. string s2="dasudgfhshelldasdhello";
    19. string s3="hello";
    20. pos=s2.find(s3,0);
    21. cout<<"字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置为:"<<pos<<endl;
    22. //4.从前往后查,查找字符串"hello"中首个与字符串"dasudgfhshelldasdhello"重复的字符位置
    23. //具体起来就是先查h是否存在于母串中,如果没有就查e是否在母串中 ,依次往下查,查到即返回下标
    24. pos=s2.find_first_of(s3);
    25. cout<<"从前往后查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:"<<pos<<endl;
    26. //5.从后往前查,查找字符串"hello"中首个与字符串"dasudgfhshelldasdhello"重复的字符位置
    27. pos=s2.find_last_of(s3) ;
    28. cout<<"从后往前查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:"<<pos<<endl;
    29. //6.查找字符串"hello"中首个与字符串"hhhhehhllloo"不重复的字符位置
    30. string str="hhhhewhhllloo";
    31. pos=str.find_first_not_of(s3);
    32. cout<<"查找字符串hello中首个与字符串hhhhehhllloo不重复的字符位置: "<<pos<<endl; //返回e的下标
    33. //7.查找字符串"hello"在字符串"hhhhehellohlllohelloo"中出现的所有位置
    34. string ss="hhhhehellohlllohelloo";
    35. string s="hello";
    36. pos=0;
    37. int i=1;
    38. cout<<"查找字符串hello在字符串hhhhehellohlllohelloo中出现的所有位置: "<<endl;
    39. while((pos=ss.find(s,pos))!=string::npos)
    40. {
    41. cout<<"第"<<i<<"个位置: "<<pos<<endl;
    42. pos++;
    43. i++;
    44. }
    45. }
    46. void replace_factor()
    47. {
    48. string s2="hello world!hello world!";
    49. s2.replace(1,3,"aaaa"); //从下标1开始去除3个字符 ,在下标位置1处插入新字符串
    50. cout<<"s2 = "<<s2<<endl;
    51. }
    52. int main()
    53. {
    54. search_factor();
    55. replace_factor();
    56. return 0;
    57. }
    58. /*
    59. 打印结果:
    60. 找到字符位置,其位置pos = 6
    61. 找到字符位置,其位置pos = 19
    62. 字符串hello在字符串dasudgfhshelldasdhello中第一次出现的位置为:17
    63. 从前往后查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:7
    64. 从后往前查,字符串hello中首个与字符串dasudgfhshelldasdhello重复的字符位置是:21
    65. 查找字符串hello中首个与字符串hhhhehhllloo不重复的字符位置: 5
    66. 查找字符串hello在字符串hhhhehellohlllohelloo中出现的所有位置:
    67. 1个位置: 5
    68. 2个位置: 15
    69. s2 = haaaao world!hello world!
    70. */

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/829791
推荐阅读
相关标签
  

闽ICP备14008679号