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