C++提取子字符串

substr()

substr() 函数可以提取 string 字符串中的子字符串,该函数有两个参数,第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout<< s1 <<endl;
    cout<< s2 <<endl;

    return 0;
}
first second third
second

该函数同样会出现参数越界的情况,如果第一个参数越界,则函数会抛出异常。在第一个参数没有越界的情况下,第二个参数仍然会导致越界,该函数的处理方式与前面提到的 erase() 函数、replace() 函数相同,子字符串最多从第一个参数所指明的下标开始一直延续到字符串结尾。

find()

find() 函数可以在字符串中查找子字符串中出现的位置。该函数有两个参数,第一个参数是待查找的子字符串,第二个参数是表示开始查找的位置,如果第二个参数不指名的话则默认从 0 开始查找,也即从字符串首开始查找。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1 = "first second third";
    string s2 = "second";
    int index = s1.find(s2,5);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;

    return 0;
}

在这里插入图片描述
rfind() 函数与 find() 函数很类似,同样是在字符串中查找子字符串。不同的是,find() 函数是从第二个参数开始往后查找,而 rfind() 函数则是最多查找到第二个参数处,如果到了第二个参数所指定的下标还没有找到子字符串,则返回一个无穷大值 4294967295。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "t second third";
    string s2 = "second";
    int index = s1.rfind(s2,6);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;

    return 0;
}

在这里插入图片描述

find_first_of()

find_first_of() 函数是用于查找子字符串和字符串共同具有的字符在字符串中出现的位置。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "first second second third";
    string s2 = "asecond";
    int index = s1.find_first_of(s2);
    if(index < s1.length())
        cout<<"aaaFound at index : "<< index <<endl;
    else
        cout<<"aaaaNot found"<<endl;

    return 0;
}

在这里插入图片描述

find_first_not_of

而 find_first_not_of() 函数则相反,它查找的是在 s1 字符串但不在 s2 字符串中的首位字符的下标,如果查找不成功则返回无穷大。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "secondasecondthird";
    string s2 = "asecond";
    int index = s1.find_first_not_of(s2);
    if(index < s1.length())
        cout<<"Found at index : "<< index <<endl;
    else
        cout<<"Not found"<<endl;
    return 0;
}

在这里插入图片描述
在本例中在 s1 但是不在 s2 中的首字符是 ‘t’,其所在下标为 13,故而返回下标 13。

C++字符串的比较

==、!=、<=、>=、<和>操作符都可以用于进行 string 类型字符串的比较,这些操作符两边都可以是 string 字符串,也可以一边是 string 字符串另一边是字符串数组。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s1 = "secondsecondthird";
    string s2 = "secondthird";
    if( s1 == s2 )
        cout<< " == " <<endl;
    if( s1 != s2 )
        cout<< " != " <<endl;
    if( s1 < s2 )
        cout<< " < " <<endl;
    if( s1 > s2 )
        cout<< " > " <<endl;

    return 0;
}

在这里插入图片描述

  • 9
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言中的find函数有多种形式,常用的有substr()、find()、find_first_of()和find_first_not_of()函数。 - substr()函数用于提取字符串中的字符串。它的语法是s.substr(pos, len),其中s是原字符串,pos是字符串的起始位置,len是字符串的长度。 - find()函数用于在字符串中查找字符串,并返回第一次出现的位置。它的语法是s.find(substring, pos),其中s是原字符串,substring是要查找的字符串,pos是开始查找的位置。如果找到了字符串,返回第一次出现的位置;如果找不到,返回string::npos。 - find_first_of()函数用于在字符串中查找第一个与指定字符集中的任意一个字符匹配的字符,并返回其位置。它的语法是s.find_first_of(chars, pos),其中s是原字符串,chars是要匹配的字符集,pos是开始查找的位置。如果找到了匹配的字符,返回其位置;如果找不到,返回string::npos。 - find_first_not_of()函数用于在字符串中查找第一个与指定字符集中的任何一个字符都不匹配的字符,并返回其位置。它的语法是s.find_first_not_of(chars, pos),其中s是原字符串,chars是要排除的字符集,pos是开始查找的位置。如果找到了不匹配的字符,返回其位置;如果找不到,返回string::npos。 所以,c find字符串的具体实现需要根据具体的需求来选择合适的函数。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C++提取字符串](https://blog.csdn.net/u014421313/article/details/127154449)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静思心远

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值