C++ 字符串类(string class)使用详解

文档声明:
以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。


资料仅供学习交流使用。
作者:Aliven888

1、简述

String class:字符串是代表字符序列的对象。

标准字符串类通过类似于标准字节容器的接口为此类对象提供支持,但增加了专门设计用于单字节字符字符串的功能。

字符串类是basic_string类模板的实例化,该模板使用char(即字节)作为其字符类型,并具有其默认的char_traits和分配器类型(有关模板的更多信息,请参见basic_string)。

请注意,此类处理字节独立于所使用的编码:如果用于处理多字节或可变长度字符(例如UTF-8)的序列,则此类的所有成员(例如长度或大小)以及 它的迭代器仍将以字节为单位(而不是实际的编码字符)进行操作。

2、接口函数

2.1、成员函数(Member functions):

名称 描述
(constructor) 构造函数 (public member function)
(destructor) 析构函数 (public member function)
operator= 运算符重载 - 赋值操作(public member function)

2.2、迭代器(Iterators):

名称 描述
begin 返回指向字符串第一个字符的向量 (public member function )
end 返回指向字符串最后一个字符后面位置的向量 (public member function)
rbegin 返回指向字符串反转后的第一个字符的向量 (public member function)
rend 返回指向字符串反转后的最后一个字符后面位置的向量 (public member function)
cbegin 返回指向字符串第一个字符的静态向量 (public member function)
cend 返回指向字符串最后一个字符后面位置的静态向量(public member function)
crbegin 返回指向字符串反转后的第一个字符的静态向量 (public member function)
crend 返回指向字符串反转后的最后一个字符后面位置的静态向量 (public member function)

2.3、容量(Capacity):

名称 描述
size 返回字符串的长度 (public member function)
length 返回字符串的长度(public member function)
max_size 返回字符串的最大长度 (public member function)
resize 重置字符串的大小 (public member function)
capacity 返回分配存储的大小 (public member function)
reserve 扩大储容量 (public member function)
clear 清空字符串 (public member function)
empty 判断字符串是否为空 (public member function)
shrink_to_fit 缩小容量,以适配实际 (public member function)

2.4、访问元素(Element access):

名称 描述
operator[] 获取字符串中的某个字符 (public member function)
at 获取字符串中的某个字符 (public member function)
back 访问字符串中的最后一个字符 (public member function)
front 访问字符串中的第一个字符 (public member function)

2.5、修改内容(Modifiers):

名称 描述
operator+= 字符串的相加 (public member function)
append 在字符串后面添加一个字符串 (public member function)
push_back 在字符串后面添加一个字符 (public member function)
assign Assign content to string (public member function)
insert Insert into string (public member function)
erase 清除字符串中的某个字符 (public member function)
replace Replace portion of string (public member function)
swap Swap string values (public member function)
pop_back Delete last character (public member function)

2.6、字符串操作(String operations):

名称 描述
c_str Get C string equivalent (public member function)
data Get string data (public member function)
get_allocator Get allocator (public member function)
copy Copy sequence of characters from string (public member function)
find Find content in string (public member function)
rfind Find last occurrence of content in string (public member function)
find_first_of Find character in string (public member function)
find_last_of Find character in string from the end (public member function)
find_first_not_of Find absence of character in string (public member function)
find_last_not_of Find non-matching character in string from the end (public member function)
substr Generate substring (public member function)
compare Compare strings (public member function)

2.7、成员变量:

名称 描述
npos Maximum value for size_t (public static member constant)

3、接口函数代码演示

3.1、Member functions

3.1.1、构造函数(constructor)

使用实例:

//定义一个string对象,可以有以下几种方式:

方式1 : 
std::string strA;   //定义一个strA字符串类型,值为空。

方式2 : 
std::string strB(strA);  //定义一个strB字符串类型,并使用strA赋值。

方式3 : 
std::string strC(strB, 2, 5);  //定义一个strC字符串类型,并使用strB位置区间在[2-5]的子串赋值。

方式4 : 
char *chA = "1234567890";
std::string strD(chA);  //定义一个strD字符串类型,并使用chA赋值。  strD = "1234567890";

方式5 : 
const char *chB = "test-string";
std::string strE(chB, 4);//定义一个strE字符串类型,并使用strB位置区间在[0 - 4]的子串赋值。 strE = "test";

方式6 : 
std::string strF(6, 'A'); //定义一个strF字符串类型,并使用 6个‘A’赋值。  strF = "AAAAAA";

方式7 : 
std::string strG(strF.begin(), strF.begin()+2); //定义一个strG字符串类型,并使用 strF 位置区间在[0 - 2]的子串  赋值。  strF = "AA";

3.1.2、析构函数(destructor)

使用实例:

//无需关心,std::string 的 对象释放时自动调用。
~string(); 

3.1.3、运算符重载(operator=)

<font color = "#e3a869">使用实例:</font>

std::string strA = "assss";   //使用默认字符串赋值。
std::string strB = strA;  //使用已存在的对象赋值。

3.2、迭代器(Iterators):

std::string 可以理解为一个容器,里面存储的都有字符,通过迭代器我们可以实现对字符串中的字符进行操作。

下面介绍下 std::string 支持的迭代器,以及使用。

std::string str = "1234567890";
    
std::string::const_iterator itBegin = str.begin();
cout << "str.begin() = " << *itBegin << endl;    //输出:str.begin() = '1'

std::string::const_iterator itEnd = str.end();
cout << "str.end() = " << *itEnd << endl;  //输出:str.end() = '\0'

std::string::reverse_iterator itrBegin = str.rbegin();
cout << "str.rbegin() = " <<  *itrBegin << endl;  //输出:str.rbegin() = '0'

std::string::reverse_iterator itrEnd = str.rend();
cout << "str.rend() = " << *itrEnd << endl;  //输出:str.end() = '\0'



std::string::const_iterator itcBegin = str.cbegin();
cout << "str.cbegin() = " << *itcBegin << endl;    //输出:str.begin() = '1'

std::string::const_iterator itcEnd = str.cend();
cout << "str.cend() = " << *itcEnd << endl;  //输出:str.end() = '\0'

std::string::const_reverse_iterator itcrBegin = str.crbegin();
cout << "str.crbegin() = " << *itcrBegin << endl;  //输出:str.rbegin() = '0'

std::string::const_reverse_iterator itcrEnd = str.crend();
cout << "str.crend() = " << *itcrEnd << endl;  //输出:str.end() = '\0'

输出打印:
注意:因为 '\0' 输出打印不显示,所以下面显示为空。

输出打印

3.3、容量(Capacity):

3.3.1、获取当前长度(size / length)

使用实例:

//返回字符串的长度(以字节为单位)。
//string :: size和string :: length都是同义词,并且返回完全相同的值。

std::string str = "1234567890";
int strSize = str.size();   //strSize = 10;
int strLen = str.size();    //strLen = 10;

3.3.2、获取最大长度(max_size)

使用实例:

//返回字符串可以达到的最大长度。
//由于已知的系统或库实现限制,这是字符串可以达到的最大潜在长度,但是不能保证对象能够达到该长度:
//在达到该长度之前,它仍然可能无法在任何时候分配存储。

std::string str = "1234567890";
int strMax_Size = str.max_size();   //strMax_Size = 10;

3.3.3、删除操作(resize)

使用实例:

//删除字符串中的字符。
//删除字符串的一部分,以减小其长度。

std::string str = "1234567890";

//删除字符串值中从字符位置pos开始并跨越len个字符的部分(如果内容太短或len是string :: npos则一直到字符串的末尾)。
//请注意,默认参数会擦除字符串中的所有字符(例如成员函数clear)。
//string& erase (size_t pos = 0, size_t len = npos);
str.erase(0, 2);     //此时 str = “34567890”;

//删除p指向的字符。
//iterator erase (const_iterator p);
std::string::iterator it = str.begin();
str.erase(it);     //此时 str = “4567890”;

//擦除[first,last)范围内的字符序列。
std::string::iterator it = str.begin();
str.erase(it, it+3);     //此时 str = “7890”;

3.3.4、获取容量(capacity)

使用实例:

//返回当前为字符串分配的存储空间的大小,以字节表示。
//此容量不一定等于字符串长度。它可以相等或更大,当将新字符添加到字符串时,多余的空间可使对象优化其操作。

std::string str = "1234567890";
int cLen = str.capacity();
cout << "str.capacity() = " << cLen << endl;

输出实例:
注意:实际长度是 10,但是 capacity() 获取的是:15

输出实例

3.3.5、扩大容量(reserve)

使用实例:

//请求根据计划的大小更改将字符串容量调整为最多n个字符的长度。
//如果n大于当前字符串容量,则该函数使容器将其容量增加到n个字符(或更大)。
//经过测试发现,重置后的容量不一定是n,也有可能是比n大的数。

std::string str = "1234567890";
cout << "str.capacity() = " << str.capacity() << endl;
str.reserve(0x10);
cout << "str.capacity() = " << str.capacity() << endl;

输出打印:
注意:经过测试发现,重置后的容量不一定是n,也有可能是比n大的数。

输出打印

3.3.6、清空内存(clear)

使用实例:

//删除字符串的内容,该内容将成为一个空字符串(长度为0个字符)。

std::string str = "1234567890";
cout << "str.length() = " << str.length() << endl;
str.clear();
cout << "str.length() = " << str.length() << endl;

输出打印:

输出打印

3.3.7、判断是否为空(empty)

使用实例:

//判断字符串是否为空
//为空返回 true, 否则返回 false

std::string str = "1234567890";
if (str.empty())
{
    cout << "str is empty." << endl;
}
else
{
    cout << "str is not empty." << endl;
}
str.clear();
if (str.empty())
{
    cout << "str is empty." << endl;
}

输出打印:

输出打印

3.3.8、缩小容量(shrink_to_fit )

使用实例:

//缩小以适合。
//请求字符串减小其容量以适合其大小。

std::string str = "1234567890";
str.reserve(0x20);
cout << "str.capacity() = " << str.capacity() << endl;
str.shrink_to_fit();
cout << "str.capacity() = " << str.capacity() << endl;

输出打印:

输出打印

3.4、元素访问(Element access)

3.4.1、获取某个字符(operator[])

使用方式:

//operator[] : 运算符重载,获取指定的字符。

std::string str = "1234567890";
cout << "str.operator[3] = " << str[3] << endl;

输出打印:

输出打印

3.4.2、获取某个字符(at)

使用方式:

//用法:
//      char& at (size_t pos);
//const char& at (size_t pos) const;

//eg:
std::string str = "1234567890";
cout << "str.at(3) = " << str.at(3) << endl;

输出打印:

输出打印

3.4.3、获取最后一个字符(back)

使用方式:

//用法:
//      char& back();
//const char& back() const;
//获取字符串的最后一个字符
//C++ 11 引入的新功能

//eg:
std::string str = "1234567890";
cout << "str.back() = " << str.back() << endl;

输出打印:

输出打印

3.4.4、获取第一个字符(front )

使用方式:

//用法:
//      char& front();
//const char& front() const;
//获取字符串的最后一个字符
//C++ 11 引入的新功能

//eg:
std::string str = "1234567890";
cout << "str.front() = " << str.front() << endl;

输出打印:

输出打印

3.5、修改内容(Modifiers)

3.5.1、字符串合并(operator+=)

使用方式:

//接口:operator+=
//  string& operator+= (const string& str);
//  string& operator+= (const char* s);
//  string& operator+= (char c);

//功能:字符串与字符串(字符)相加

//eg:
std::string strA = "12345";
std::string strB = "67890";
const char *chpBuf = "AAAA";
char chBuf = 'B';

strA += strB;
cout << "strA += strB  --> " << strA << endl;

strA += chpBuf;
cout << "strA += chpBuf  --> " << strA << endl;

strA += chBuf;
cout << "strA += chBuf   --> " << strA << endl;

输出打印:

输出打印

3.5.2、附加字符或者字符串(append)

使用方式:

//接口:
//  string& append (const string& str);
//  string& append (const string& str, size_t subpos, size_t sublen);
//  string& append (const char* s);
//  string& append (const char* s, size_t n);
//  string& append (size_t n, char c);
//  template <class InputIterator>
//    string& append (InputIterator first, InputIterator last);
//
//功能:在字符串后面添加一个字符或者字符串

//eg:
std::string strA = "12345";
std::string strB = "67890";
std::string strC = "ABCDE";
std::string strD = "FGHIJ";
const char *chpBuf = "KLMNO";

strA.append(strB);
cout << "strA.append(strB) = " << strA << endl;

strA.append(strC, 1, 3);
cout << "strA.append(strC, 1, 3) = " << strA << endl;

strA.append(chpBuf);
cout << "strA.append(chpBuf) = " << strA << endl;

strA.append(chpBuf, 3);
cout << "strA.append(chpBuf, 3) = " << strA << endl;

strA.append(3, 'H');
cout << "strA.append(3, 'H') = " << strA << endl;

strA.append(strD.begin(), strD.begin()+2);
cout << "strA.append(strD.begin(), strD.begin()+2) = " << strA << endl;

输出打印:

输出打印

3.5.3、末尾添加字符(push_back)

使用方式:

//接口:
// void push_back (char c);
//
//功能:在字符串末尾添加一个字符

//eg:
std::string strA = "12345";
strA.push_back('H');
cout << "strA.push_back('H') = " << strA << endl;

输出打印:

输出打印

3.5.4、内容分配(assign)

使用方式:

//接口:
//  string& assign(const string& str);
//  string& assign(const string& str, size_t subpos, size_t sublen);
//  string& assign(const char* s);
//  string& assign(const char* s, size_t n);
//  string& assign(size_t n, char c);
//  template <class InputIterator>
//      string& assign(InputIterator first, InputIterator last);

//功能:为字符串分配一个新值,替换其当前内容。

//eg:
std::string strA = "12345";
std::string strB = "67890";
std::string strC = "ABCDE";
std::string strD = "FGHIJ";
const char *chpBuf = "KLMNO";

strA.assign(strB);
cout << "strA.assign(strB) = " << strA << endl;

strA.assign(strC, 1, 3);
cout << "strA.assign(strC, 1, 3) = " << strA << endl;

strA.assign(chpBuf);
cout << "strA.assign(chpBuf) = " << strA << endl;

strA.assign(chpBuf, 3);
cout << "strA.assign(chpBuf, 3) = " << strA << endl;

strA.assign(3, 'H');
cout << "strA.assign(3, 'H') = " << strA << endl;

strA.assign(strD.begin(), strD.begin()+2);
cout << "strA.assign(strD.begin(), strD.begin()+2) = " << strA << endl;

输出打印:

输出打印

3.5.5、内容插入(insert)

使用方式:

//接口:
//  string& insert(size_t pos, const string& str);
//  string& insert(size_t pos, const string& str, size_t subpos, size_t sublen);
//  string& insert(size_t pos, const char* s);
//  string& insert(size_t pos, const char* s, size_t n);
//  string& insert(size_t pos, size_t n, char c);
//     void insert(iterator p, size_t n, char c);
//  iterator insert(iterator p, char c);
//  template <class InputIterator>
//     void insert(iterator p, InputIterator first, InputIterator last);

//功能:在字符串中,pos(或p)指向的字符之前,插入其他字符

//eg:
std::string strA = "12345";
std::string strB = "67890";
std::string strC = "ABCDE";
std::string strD = "FGHIJ";
const char *chpBuf = "KLMNO";

strA.insert(2, strB);
cout << "strA.insert(2, strB) = " << strA << endl;

strA.insert(2, strC, 1, 3);
cout << "strA.insert(2, strC, 1, 3) = " << strA << endl;

strA.insert(2, chpBuf);
cout << "strA.insert(2, chpBuf) = " << strA << endl;

strA.insert(2, chpBuf, 3);
cout << "strA.insert(2, chpBuf, 3) = " << strA << endl;

strA.insert(2, 3, 'H');
cout << "strA.insert(2, 3, 'H') = " << strA << endl;

strA.insert(strA.begin(), 3, 'W');
cout << "strA.insert(strA.begin(), 3, 'W') = " << strA << endl;

strA.insert(strA.begin(), 'Q');
cout << "strA.insert(strA.begin(), 'Q') = " << strA << endl;

strA.insert(strA.begin(), strD.begin(), strD.end());
cout << "strA.insert(strA.begin(), strD.begin(), strD.end()) = " << strA << endl;

image.png

3.5.6、清除子串(erase)

使用方式:

//接口:
//  string& erase(size_t pos = 0, size_t len = npos);
//  iterator erase(iterator p);
//  iterator erase(iterator first, iterator last);

//功能:清除字符串的一部分,以减小其长度。

//eg:
std::string strA = "1234567890";
strA.erase(0, 2);
cout << "strA.erase(0, 2) = " << strA << endl;

strA = "1234567890";  //还原字符串值
strA.erase(strA.begin()+2);
cout << "strA.erase(strA.begin()+2) = " << strA << endl;

strA = "1234567890";  //还原字符串值
strA.erase(strA.begin() + 2, strA.begin() + 5);
cout << "strA.(strA.begin() + 2, strA.begin() + 5) = " << strA << endl;

输出打印:

image.png

3.5.7、内容替换(replace)

使用方式:

//接口:
// string& replace(size_t pos, size_t len, const string& str);
// string& replace(iterator i1, iterator i2, const string& str);
// string& replace(size_t pos, size_t len, const string& str,size_t subpos, size_t sublen);
// string& replace(size_t pos, size_t len, const char* s);
// string& replace(iterator i1, iterator i2, const char* s);
// string& replace(size_t pos, size_t len, const char* s, size_t n);
// string& replace(iterator i1, iterator i2, const char* s, size_t n);
// string& replace(size_t pos, size_t len, size_t n, char c);
// string& replace(iterator i1, iterator i2, size_t n, char c);
// template <class InputIterator>
//   string& replace(iterator i1, iterator i2, InputIterator first, InputIterator last);

//功能:用新内容替换以字符pos开头并跨越len个字符(或[i1,i2之间的范围内的字符串部分])的字符串部分:

//eg:
{
std::string strA = "1234567890";
std::string strB = "ABCDEFGHIJ";
const char *chpBuf = "@#$%&*()[?";


strA = "1234567890";
strA.replace(1, 2, strB);
cout << "strA.replace(1, 2, strB) = " << strA << endl;

strA = "1234567890";
strA.replace(strA.begin(), strA.begin() + 2, strB);
cout << "strA.replace(strA.begin(), strA.begin() + 2, strB) = " << strA << endl;

strA = "1234567890";
strA.replace(1, 2, strB, 3, 7);
cout << "strA.replace(1, 2, strB, 3, 7) = " << strA << endl;

strA = "1234567890";
strA.replace(1, 2, chpBuf);
cout << "strA.replace(1, 2, chpBuf) = " << strA << endl;

strA = "1234567890";
strA.replace(strA.begin()+2, strA.begin()+3, chpBuf);
cout << "strA.replace(strA.begin()+2, strA.begin()+3, chpBuf) = " << strA << endl;

strA = "1234567890";
strA.replace( 5, 3, chpBuf, 5);
cout << "strA.replace(strA.begin()+2, strA.begin()+3, chpBuf) = " << strA << endl;

strA = "1234567890";
strA.replace(strA.begin(), strA.begin() + 2, chpBuf, 5);
cout << "strA.replace(strA.begin(), strA.begin() + 2, chpBuf, 5) = " << strA << endl;

strA = "1234567890";
strA.replace(5, 3, 5, 'G');
cout << "strA.replace(5, 3, 5, 'G') = " << strA << endl;

strA = "1234567890";
strA.replace(strA.begin() + 2, strA.begin() + 3, 6, 'S');
cout << "strA.replace(strA.begin() + 2, strA.begin() + 3, 6, 'S') = " << strA << endl;

strA = "1234567890";
strA.replace(strA.begin() + 2, strA.begin() + 3,  strB.begin(), strB.end());
cout << "strA.replace(strA.begin() + 2, strA.begin() + 3,  strB.begin(), strB.end()) = " << strA << endl;

输出打印:

image.png

3.5.8、内容交换(swap)

使用方式:

//接口:
//void swap (string& str);

//功能:用另一个字符串对象str的内容交换容器的内容。 长度可能会有所不同。

//eg:
std::string strA = "1234567890";
strA.swap(strB);
cout << "strA.swap(strB) = " << strA << endl;

输出打印:

image.png

3.5.9、删除最后一个字符(pop_back )

使用方式:

//接口:
//void pop_back();

//功能:删除字符串的最后一个字符,有效地将其长度减少一个。

//eg:
std::string strA = "1234567890";
strA.pop_back();
cout << "strA.pop_back() = " << strA << endl;

输出打印:

image.png

3.6、字符串操作(String operations):

3.6.1、转化成指针类型(c_str())

使用方式:

//接口:
//const char* c_str() const;
//
//功能:返回指向一个数组的指针,该数组包含一个以空值终止的字符序列(即C字符串),代表字符串对象的当前值。

//eg:
std::string strA = "1234567890";
const char *chA = strA.c_str();
cout << "strA.c_str() = " << chA << endl;

输出打印:

image.png

3.6.2、转化成指针类型(data)

使用方式:

//接口:
//const char* data() const;

//功能:返回一个指向数组的指针,该数组包含与组成字符串对象值的字符相同的字符序列。

//eg:
const char *chA = strA.data();
cout << "strA.data() = " << chA << endl;

输出打印:

image.png

3.6.3、复制子串(copy)

使用方式:

//接口:
//size_t copy (char* s, size_t len, size_t pos = 0) const;

//功能:复制字符串中的字符子串。
//将字符串对象当前值的子字符串复制到s指向的数组中。 
//该子字符串包含从位置pos开始的len个字符。

//eg:
std::string strA = "1234567890";
char chpBuf[20] = { '\0' };
strA.copy(chpBuf, 2, 3);
cout << "strA.copy() = " << chpBuf << endl;

输出打印:

image.png

3.6.4、查找第一次出现位置(find)

使用方式:

//接口:
//  size_t find(const string& str, size_t pos = 0) const noexcept;
//  size_t find(const char* s, size_t pos = 0) const;
//  size_t find(const char* s, size_t pos, size_type n) const;
//  size_t find(char c, size_t pos = 0) const noexcept;


//str
//要搜索的字符串。

//pos
//搜索中要考虑的字符串中第一个字符的位置(搜索的起点)。
//如果该长度大于字符串长度,则该函数将永远找不到匹配项。
//注意:第一个字符由0值表示(不是1):0值表示搜索整个字符串。

//s
//指向字符数组的指针。
//如果指定了参数n(3),则要匹配的序列是数组中的前n个字符。
//否则(2),期望以空字符结尾的序列:要匹配的序列的长度由首次出现的空字符确定。

//n
//要匹配的字符序列的长度。

//C
//要搜索的单个字符。

//功能:在字符串内查找目标字符子串或者字符,并返回查到的第一个位置。

//如果指定了pos,则搜索仅在位置pos或之后的字符,
//而忽略包括pos之前的任何可能出现的字符。

//eg:
std::string strA = "1234567890";
std::string strB = "34";
const char *chpBuf = "78";
cout << "strA.find(strB) = " << strA.find(strB) << endl;
cout << "strA.find(chpBuf) = " << strA.find(chpBuf) << endl;

|rfind|Find last occurrence of content in string (public member function)|

3.6.5、查找最后一次出现位置(rfind)

使用方式:

//接口:
size_t rfind(const string& str, size_t pos = npos) const noexcept;
size_t rfind(const char* s, size_t pos = npos) const;
size_t rfind(const char* s, size_t pos, size_t n) const;
size_t rfind(char c, size_t pos = npos) const noexcept;


str
待搜索的字符子串。

pos
字符串中最后一个字符的位置将被视为匹配的开始。
任何大于或等于字符串长度的值(包括string :: npos)意味着将搜索整个字符串。
注意:第一个字符由0值表示(不是1)。

s
指向字符数组的指针。
如果指定了参数n(3),则要匹配的序列是数组中的前n个字符。
否则(2),期望以空字符结尾的序列:要匹配的序列的长度由首次出现的空字符确定。

n
待匹配的字符序列的长度。

C
待搜索的单个字符。

//功能:
//查找字符串中最后出现的位置。
//如果指定了pos,则搜索仅包括在位置pos或之前开始的字符序列,
//而忽略从pos之后开始的任何可能的匹配。

3.6.6、查找第一次出现位置(find_first_of)

使用方式:

//接口:
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;    
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const noexcept;

str
待搜索的字符子串。

pos
字符串中最后一个字符的位置将被视为匹配的开始。
任何大于或等于字符串长度的值(包括string :: npos)意味着将搜索整个字符串。
注意:第一个字符由0值表示(不是1)。

s
指向字符数组的指针。
如果指定了参数n(3),则要匹配的序列是数组中的前n个字符。
否则(2),期望以空字符结尾的序列:要匹配的序列的长度由首次出现的空字符确定。

n
待匹配的字符序列的长度。

C
待搜索的单个字符。

功能:
在字符串中搜索指定字符后者字符子串的第一个位置。
如果指定了pos,则搜索仅包括位置pos或之后的字符,而忽略pos之前可能出现的任何字符。

3.6.7、查找最后一次出现位置(find_last_of)

使用方式:

//接口:
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const noexcept;

str
待搜索的字符子串。

pos
字符串中最后一个字符的位置将被视为匹配的开始。
任何大于或等于字符串长度的值(包括string :: npos)意味着将搜索整个字符串。
注意:第一个字符由0值表示(不是1)。

s
指向字符数组的指针。
如果指定了参数n(3),则要匹配的序列是数组中的前n个字符。
否则(2),期望以空字符结尾的序列:要匹配的序列的长度由首次出现的空字符确定。

n
待匹配的字符序列的长度。

C
待搜索的单个字符。

功能:
在字符串中搜索指定字符或者字符子串出现最后一次的位置。
如果指定了pos,则搜索仅包括位置pos或之前的字符,而忽略pos之后的任何可能出现的情况。

3.6.8、查找第一个不匹配(find_first_not_of)

使用方式:

//接口:
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
size_t find_first_not_of (char c, size_t pos = 0) const noexcept;

str
另一个字符串,其中包含要在目标字符串中搜索使用的字符集。

pos
搜索中要考虑的字符串中第一个字符的位置。
如果该长度大于字符串长度,则该函数将永远找不到匹配项。
注意:第一个字符由0值表示(不是1):0值表示搜索整个字符串。

s
指向字符数组的指针。
如果指定了参数n(3),则在搜索中使用数组中的前n个字符。
否则(2),期望以空字符结尾的序列:具有在搜索中使用的字符的序列的长度由首次出现的空字符确定。

n
要搜索的字符值的数量。

C
要搜索的单个字符。

功能:
在字符串中搜索与字符集中指定的任何一个字符都不匹配的第一个字符的位置。

如果指定了pos,则搜索仅包括位置pos或之后的字符,而忽略该字符之前的任何可能出现的情况。

eg:
std::string strA = "1234567890";
cout << "strA.find_first_not_of(\"32154r56\") = " << strA.find_first_not_of("32154r56") << endl;

输出打印:
字符 ‘7’ 不在字符串 “"32154r56” 中, 而字符 ‘7’ 的位置是 ipos = 6.

image.png

3.6.9、查找最后一个不匹配(find_last_not_of)

使用方式:

//接口:
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const noexcept;

str
另一个包含要搜索字符的字符串。

pos
搜索中要考虑的字符串中最后一个字符的位置。
任何大于或等于字符串长度的值(包括string :: npos)都表示要搜索整个字符串。
注意:第一个字符由0值表示(不是1)。

s
指向字符数组的指针。
如果指定了参数n(3),则搜索数组中的前n个字符。
否则(2),期望以空字符结尾的序列:具有匹配字符的序列的长度由首次出现的空字符确定。

n
要搜索的字符值的数量。

C
要搜索的单个字符。



功能:
在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。
如果指定了pos,则搜索仅包括位置pos或之前的字符,而忽略pos之后的任何可能出现的情况。
匹配的最后一个字符的位置。
如果未找到匹配项,则该函数返回string :: npos。

//eg:
std::string strA = "123456789055F";
cout << "strA.find_last_not_of(\"54rsd\") = " << strA.find_last_not_of("54rsd3") << endl;

输出打印:
字符 ‘F’ 不在字符串 "54rsd3” 中, 而字符 ‘F’ 的位置是 ipos = 12.

image.png

3.6.10、获取字符子串(substr)

使用方式:

//接口:
string substr (size_t pos = 0, size_t len = npos) const;

pos
表示开始获取的起点。

len
表示获取的长度。

//功能:获取指定位置指定长度的字符子串。

//eg:
std::string strA = "123456789055F";
cout << "strA.substr(2, 5) = " << strA.substr(2, 5) << endl;

输出打印:

image.png

3.6.11、字符串比较(compare)

<font color = "#e3a869">使用方式:</font>

//接口:
int compare (const string& str) const noexcept;

int compare (size_t pos, size_t len, const string& str)const;
int compare (size_t pos, size_t len, const string& str,
             size_t subpos, size_t sublen) const;
    
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
int compare (size_t pos, size_t len, const char* s, size_t n) const;

str
另一个字符串对象,完全(或部分)用作比较字符串。

pos
比较字符串中第一个字符的位置。
如果该长度大于字符串长度,则抛出out_of_range。
注意:第一个字符由0值表示(不是1)。

len
比较的字符串的长度(如果字符串较短,则尽可能多的字符)。
string :: npos的值表示直到字符串末尾的所有字符。

subpos,sublen
与上面的pos和len相同,但用于比较字符串。

s
指向字符数组的指针。
如果指定了参数n(4),则将数组中的前n个字符用作比较字符串。
否则(3),将期望以空字符结尾的序列:具有用作比较字符串的字符的序列的长度由空字符的第一次出现来确定。

n
要比较的字符数。

功能:将字符串对象(或子字符串)的值与其参数指定的字符序列进行比较。

3.7、成员变量:

3.7.1、npos

使用方式:
npos 是字符串的最大长度,std::string::npos 表示字符串的末尾位置。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335