std::string::substr

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 str
	std::string str3 = str.substr(pos);      // get from "live" to the end

	std::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, throws
		std::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

发布了473 篇原创文章 · 获赞 1762 · 访问量 104万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104505378