本文共 4840 字,大约阅读时间需要 16 分钟。
字符串类型(String)是 C++ 标准库中用来表示可变长字符序列的组件。要使用 string 类型,需在代码中包含 string 头文件(
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcd"; for (int i = 0; i < str.length(); i++) { cout << static_cast (str[i]) << " "; } return 0;}
输出示例:
a b c d
注意:对于字符串操作,可以通过 cin 和 cout 读取和输出整个字符串。若要逐个字符处理,才需要使用下标。
通过 c_str() 方法可以将 string 类型转换为 C 语言中的字符串数组进行输出。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcd"; cout << str.c_str() << endl; return 0;}
输出示例:
abcd
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcd"; for (string::iterator it = str.begin(); it != str.end(); ++it) { cout << static_cast (*it) << " "; } return 0;}
注意:在 STL 中,只有 string 和 vector 类支持直接对迭代器进行加减某个数字操作,如 str.begin() + 3。
两个 string 类型可以直接拼接。
C++ 代码示例:
#include#include using namespace std;int main() { string str1 = "abc", str2 = "xyz", str3; str3 = str1 + str2; str1 += str2; cout << str1 << endl; cout << str3 << endl; return 0;}
输出示例:
abcxyzabcxyz
可以使用 ==、!=、<、<=、>、>= 等比较 string 类型的大小,比较规则基于字典序。
C++ 代码示例:
#include#include using namespace std;int main() { string str1 = "abc", str2 = "xyz"; if (str1 == str2) { cout << "两者相等" << endl; } else { cout << "不相等" << endl; } return 0;}
两者返回字符串的长度,可以使用任之一:
C++ 代码示例:
#include#include using namespace std;int main() { string str1 = "abc", str2 = "xyz", str3; str3 = str1 + str2; str1 += str2; cout << str1 << endl; cout << str3 << endl; cout << str1.length() << endl; cout << str1.size() << endl; return 0;}
输出示例:
abcxyzabcxyz77
可以在指定位置插入子字符串。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcxyz", str2 = "opq"; str.insert(3, str2); cout << str << endl; return 0;}
注意:str2 或者迭代器范围可以作为插入内容的来源。
例如,使用迭代器:
string str = "abcxyz", str2 = "opq";str.insert(str.begin() + 3, str2.begin(), str2.end());
输出示例:
abcopqxyz
用于删除单个或多个字符。
C++ 代码示例(删除单个字符):
#include#include using namespace std;int main() { string str = "abcxyz", str2 = "opq"; str.erase(str.begin() + 4); cout << str << endl; return 0;}
输出示例:
abxyz
另一个示例(删除特定区间):
#include#include using namespace std;int main() { string str = "abcdefg"; str.erase(str.begin() + 2, str.end() - 1); cout << str << endl; return 0;}
输出示例:
abcdef
传入起始位置和要删除的字符个数。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcdefg"; str.erase(3, 2); cout << str << endl; return 0;}
输出示例:
abcd
用于清空字符串中的所有数据。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "abcdefg"; str.clear(); cout << str.length() << endl; return 0;}
输出示例:
0
用于提取子字符串。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "Welcome to Changsha!"; cout << str.substr(0, 5) << endl; cout << str.substr(14, 4) << endl; return 0;}
输出示例:
Welcomesha!
表示无效子字符串的位置,值为 -1。
C++ 代码示例:
#include#include using namespace std;int main() { if (string::npos == -1) { cout << "-1 is true." << endl; } return 0;}
输出示例:
-1是真实的。
用于寻找子字符串的位置,返回 string::npos 若不存在。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "Thank you for your smile."; string str2 = "you"; string str3 = "me"; if (str.find(str2) != string::npos) { cout << str.find(str2) << endl; } if (str.find(str2, 7) != string::npos) { cout << str.find(str2, 7) << endl; } if (str.find(str3) != string::npos) { cout << str.find(str3) << endl; } else { cout << "I know there is no position for me." << endl; } return 0;}
输出示例:
715-1是真实的。
在调用 find 方法前,建议先测试子字符串是否存在于主字符串中,以避免莫须有情况。
用于替换字符串中特定范围内的子串。
C++ 代码示例:
#include#include using namespace std;int main() { string str = "Maybe you will turn around."; string str2 = "will not"; string str3 = "surely"; cout << str.replace(10, 4, str2) << endl; cout << str.replace(str.begin(), str.begin() + 5, str3) << endl; return 0;}
输出示例:
Maybe you will not.you.rely
通过以上实例可以发现,string 类 提供了丰富的操作方法,使得字符串处理变得高效和直观。在开发中,合理使用这些方法可以提高代码的可维护性和可读性。随着对 C++ 标准库的深入理解,可以进一步发掘更多高级功能,提升编程能力。
转载地址:http://bvynz.baihongyu.com/