std::string::substr

Defined in header <string> - 定义于头文件 <string>

public member function - 公开成员函数

mutex:n. 互斥,互斥元,互斥体,互斥量
synchronization [ˌsɪŋkrənaɪˈzeɪʃn]:n. 同步,同时性
primitive [ˈprɪmətɪv]:adj. 原始的,远古的,简单的,粗糙的 n. 原始人
simultaneously [ˌsɪmlˈteɪniəsli]:adv. 同时地
exclusive [ɪkˈskluːsɪv]:adj. 独有的,排外的,专一的 n. 独家新闻,独家经营的项目,排外者
semantics [sɪˈmæntɪks]:n. 语义学,语义论
recursive [rɪˈkɜːsɪv]:adj. 递归的,循环的

1. std::string::substr

string substr (size_t pos = 0, size_t len = npos) const;

Generate substring - 产生子字串

Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
返回一个新构造的字符串对象,其值初始化为该对象的子字符串的副本。

The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).
子字符串是对象的一部分,从字符位置 pos 开始并跨越 len 个字符 (或直到字符串的结尾,以先到者为准)。

返回子串 [pos, pos+len)。若请求的子串越过 string 的结尾。

2. Parameters

pos
Position of the first character to be copied as a substring.
要作为子字符串复制的第一个字符的位置。

If this is equal to the string length, the function returns an empty string.
如果它等于字符串长度,则该函数返回一个空字符串。

If this is greater than the string length, it throws out_of_range.
如果该长度大于字符串长度,则抛出 out_of_range

Note: The first character is denoted by a value of 0 (not 1).
注意:第一个字符由 0 表示 (不是 1)。

要包含的首个字符的位置。

len
Number of characters to include in the substring (if the string is shorter, as many characters as possible are used).
子字符串中包含的字符数 (如果字符串较短,则使用尽可能多的字符)。

A value of string::npos indicates all characters until the end of the string.
string::npos 的值表示直到字符串末尾的所有字符。

size_t is an unsigned integral type (the same as member type string::size_type).
size_t 是一个无符号整数类型 (与成员类型 string::size_type 相同)。

3. Return value

A string object with a substring of this object.

4. Example - 示例

4.1 std::string::substr

//============================================================================
// Name        : std::string::substr
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <iostream>
#include <string>int main()
{std::string str = "We think in generalities, but we live in details.";std::string str2 = str.substr(3, 5);     // "think"std::size_t pos = str.find("live");      // position of "live" in strstd::string str3 = str.substr(pos);      // get from "live" to the endstd::cout << str2 << '\n' << str3 << '\n';return 0;
}
think
live in details.

4.2 std::string::substr

//============================================================================
// Name        : std::string::substr
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <string>
#include <iostream>int main()
{std::string a = "0123456789abcdefghij";// count is npos, returns [pos, size())std::string sub1 = a.substr(10);std::cout << sub1 << '\n';// both pos and pos+count are within bounds, returns [pos, pos+count)std::string sub2 = a.substr(5, 3);std::cout << sub2 << '\n';// pos is within bounds, pos+count is not, returns [pos, size())std::string sub4 = a.substr(a.size() - 3, 50);std::cout << sub4 << '\n';try{// pos is out of bounds, throwsstd::string sub5 = a.substr(a.size() + 3, 50);std::cout << sub5 << '\n';} catch (const std::out_of_range& e){std::cout << "pos exceeds string size\n";}return 0;
}
abcdefghij
567
hij
pos exceeds string size

5. Complexity

Unspecified, but generally linear in the length of the returned object.
未指定,但是返回对象的长度通常是线性的。

6. Iterator validity

No changes.

7. Data races - 数据竞争

The object is accessed.
对象被访问。

8. Exception safety - 异常安全性

Strong guarantee: if an exception is thrown, there are no changes in the string.
强有力的保证:如果引发异常,则字符串中没有任何变化。

If pos is greater than the string length, an out_of_range exception is thrown.
如果 pos 大于字符串长度,则抛出 out_of_range 异常。

A bad_alloc exception is thrown if the function needs to allocate storage and fails.
如果函数需要分配存储并失败,则会引发 bad_alloc 异常。

Reference

http://www.cplusplus.com/reference/string/string/substr/
https://en.cppreference.com/w/cpp/string/basic_string/substr

std::string::substr相关推荐

  1. C++ std::string::substr()

    substr()函数返回一个新建的 初始化为string对象的子串的拷贝string对象. 子串是,在字符位置_Off开始,跨越_Count个字符(或直到字符串的结尾)对象的部分 void main( ...

  2. C++ string substr()

    共有成员函数 <string> std::string::substr string substr (size_t pos = 0, size_t len = npos) const; 产 ...

  3. 用c++实现分割字符串函数(分割std::string)

    2018.09.21更新于文章尾部:第二个函数优化了处理时间,占用更少内存和时间 2020.05.07更新于前两个函数:加入了说明注释,方便理解 2022.06.06更新一个比较巧妙的方法<Ge ...

  4. C++/C++11中std::string用法汇总

    C++/C++11中std::string是个模板类,它是一个标准库.使用string类型必须首先包含<string>头文件.作为标准库的一部分,string定义在命名空间std中. st ...

  5. std::string用法总结

    c++stdstring 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询.在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(const ...

  6. std::string 用法

    string类的构造函数: string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化 string类的字符操作: cons ...

  7. std:: string 的常用使用

    std string 的常用使用 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.Define string s1 = "hello&qu ...

  8. C++ 标准程序库std::string 详解

    现在一般不再使用传统的char*而选用C++标准程序库中的string类,是因为string标准程序和char*比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,集成的操作函数足以完 ...

  9. std::string与MFC的CString的比较

    关于CString的详细用法,参见:www.cnblogs.com/htj10/p/9341545.html 关于std::string的详细用法,参见:http://www.cplusplus.co ...

最新文章

  1. Spring Boot如何初始化数据
  2. 微软拟对Office 2007增添新功能 增强安全性
  3. 红帽Redhat6.4 iso下载地址
  4. 开发时对业务技术框架的理解
  5. Redis布隆过滤器
  6. 系统鸿蒙生态链,任正非透露鸿蒙系统细节,比安卓速度快60%,两三年建好生态链...
  7. python实例属性引用-python之对象(实例)
  8. Redis进阶-高可用:集群
  9. 显示android 运行时异常,在android中获取运行时异常
  10. 用python画漂亮图-零基础用 Python 画图表,让你的论文更美观
  11. 20191127每日一句
  12. 海龟编辑器 html版,海龟编辑器官方版
  13. MySQL:JDBC基础及连接数据库的方式
  14. 免费计算机网络基础ppt,计算机网络基础概念课件.ppt
  15. WX计数器统计器使用教程
  16. 黑群晖折腾之安装Transmission下载有速度但是会自动暂停
  17. 毕业季怎么做答辩PPT?
  18. 【计算机网络】计算机网络的组成
  19. 【Android -- Material Design】CollapsingToolbarLayout 的基本使用
  20. Android调查问卷的创建与提交

热门文章

  1. 【9505】部落卫队
  2. Collection和List,Set,Map的关系与说明
  3. Git帝国之tag大臣
  4. 动作捕捉系统用于苹果采摘机器人
  5. 如何清洁你脏兮兮的笔记本电脑?
  6. 响铃和振动(转载的)
  7. 【网络】远程连接路由器
  8. Javascript与多线程
  9. 如何把oracle11g卸载干净
  10. PCA 降维 + 基于轮廓系数确定K-Means最优簇数