c++string字符串截取详解

一、c截取字符串

c语言中可以使用strtok函数截取字符串,但是在c++中,可以使用string中的substr函数实现截取字符串。

substr函数接受两个参数,第一个参数为截取的起始下标,第二个参数为要截取的长度。如果只传入一个参数,则截取剩余的全部字符串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = str.substr(6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

二、cstring截取字符串

cstring是c++中对于c语言中char*类型字符串的封装。使用cstring截取字符串可以使用类似于c语言中的指针进行截取。

可以通过对于字符串名的取地址,来获取字符串的首地址。利用指针可以直接操作字符串。

#include 
#include 
using namespace std;

int main() {
    char str[] = "hello world";
    char* sub_str = str + 6;
    cout << sub_str << endl;  // "world"
    return 0;
}

三、截取字符串string

在string中截取字符串也可以使用substr函数,其实现方式与c截取字符串类似。

需要注意的是,在实际使用中,可以采用string对象的方式截取字符串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = string(str, 6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

四、c截取字符串的指定字符

c语言中使用strchr函数来查找字符串中的指定字符,而在c++中可以使用find函数来实现。

find函数返回指定字符在字符串中第一次出现的位置。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    int index = str.find('w');
    string sub_str = str.substr(index);
    cout << sub_str << endl;  // "world"
    return 0;
}

五、std::string截取字符串

std::string和string的使用差不多,只是可能需要加上命名空间std::来使用。

std::string的substr函数用法和普通的string类似。

#include 
#include 
using namespace std;

int main() {
    std::string str = "hello world";
    std::string sub_str = str.substr(6, 5);
    cout << sub_str << endl;  // "world"
    return 0;
}

六、string截取指定字符串

使用string的find函数获取子字符串在主字符串中的位置,再通过substr函数截取。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = "world";
    int index = str.find(sub_str);
    string result = str.substr(index);
    cout << result << endl;  // "world"
    return 0;
}

七、string类型截取字符串

可以使用string的find函数和substr函数实现截取字符串。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string sub_str = "wo";
    int start = str.find(sub_str)+sub_str.size();
    string result = str.substr(start);
    cout << result << endl;  // "rld"
    return 0;
}

八、cstring截取到指定字符

和c语言中类似,使用strchr函数查找指定字符的位置,然后进行指针的操作实现字符串的截取。

#include 
#include 
using namespace std;

int main() {
    char str[] = "hello, world";
    char* sub_str = strchr(str, ',');
    *sub_str = '\0';
    cout << str << endl;  // "hello"
    return 0;
}

九、string截取两个字符串之间的值

可以使用string的find_first_of和find_last_of函数查找两个字符串的位置,然后通过substr函数进行截取。

#include 
#include 
using namespace std;

int main() {
    string str = "hello world";
    string start_str = "h";
    string end_str = "d";
    int start = str.find_first_of(start_str)+1;
    int end = str.find_last_of(end_str);
    string result = str.substr(start, end-start);
    cout << result << endl;  // "ello worl"
    return 0;
}

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/242399.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-12 12:49
下一篇 2024-12-12 12:49

相关推荐

  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • Python中将字符串转化为浮点数

    本文将介绍在Python中将字符串转化为浮点数的常用方法。在介绍方法之前,我们先来思考一下这个问题应该如何解决。 一、eval函数 在Python中,最简单、最常用的将字符串转化为…

    编程 2025-04-29
  • Java判断字符串是否存在多个

    本文将从以下几个方面详细阐述如何使用Java判断一个字符串中是否存在多个指定字符: 一、字符串遍历 字符串是Java编程中非常重要的一种数据类型。要判断字符串中是否存在多个指定字符…

    编程 2025-04-29
  • Python学习笔记:去除字符串最后一个字符的方法

    本文将从多个方面详细阐述如何通过Python去除字符串最后一个字符,包括使用切片、pop()、删除、替换等方法来实现。 一、字符串切片 在Python中,可以通过字符串切片的方式来…

    编程 2025-04-29
  • c# enum转换成string

    本文将从以下几个方面详细阐述c#中enum类型转换成string类型的方法及注意事项。 一、基本语法和示例 c#中的enum类型可以看作是一组有名字的常量值,通常用于定义一组相关的…

    编程 2025-04-29
  • Python如何将字符串1234变成数字1234

    Python作为一种广泛使用的编程语言,对于数字和字符串的处理提供了很多便捷的方式。如何将字符串“1234”转化成数字“1234”呢?下面将从多个方面详细阐述Python如何将字符…

    编程 2025-04-29
  • Python int转二进制字符串

    本文将从以下几个方面对Python中将int类型转换为二进制字符串进行详细阐述: 一、int类型和二进制字符串的定义 在Python中,int类型表示整数,二进制字符串则是由0和1…

    编程 2025-04-29
  • 用title和capitalize美观处理Python字符串

    在Python中,字符串是最常用的数据类型之一。对字符串的美观处理是我们在实际开发中经常需要的任务之一。Python内置了一些方法,如title和capitalize,可以帮助我们…

    编程 2025-04-28
  • Python 提取字符串中的电话号码

    Python 是一种高级的、面向对象的编程语言,它具有简单易学、开发迅速、代码简洁等特点,广泛应用于 Web 开发、数据科学、人工智能等领域。在 Python 中,提取字符串中的电…

    编程 2025-04-28
  • Python如何打印带双引号的字符串

    Python作为一种广泛使用的编程语言,在日常开发中经常需要打印带双引号的字符串。那么,如何打印带双引号的字符串呢? 一、使用转义字符 在Python中,我们可以通过使用转义字符\…

    编程 2025-04-28

发表回复

登录后才能评论