天天看点

C++之String标准类库

C++之String标准类库

基础

#include <iostream>
#include <string>
using namespace std;

/*
	初始化方式:拷贝初始化、直接初始化
		string s1 = "haha"; // 拷贝初始化
		string s2("haha");  // 直接初始化

*/

void testString() {
	cout << "string标准库:" << endl;

	string s1;
	string s2(s1), s3("value"), s4(10, 'a');
	// s2 = s1, s3 = "value", s4 = "aaaaaaaaaa"

	s1 = "I am a student!";
	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;

	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl;
	
	for (int i = 0; i < s1.size(); i++) {
		cout << s1[i] << " "; // 遍历字符串的每一个字符,index从0开始,s1.size()返回字符串长度
	}
	cout << "s1's length: " << s1.size() << endl;
	cout << "s2's length: " << s2.size() << endl;

	cout << endl;
	cout << "s1 is empty? " << s1.empty() << endl;  // s1.empty()判断字符串是否为空,是返回1,否返回0
	cout << "s2 is empty? " << s2.empty() << endl;

	cout << "s1+s3: " << s1 + s3 << endl;  // 字符串直接相加

	s1 == s2 ? printf("true\n") : printf("false\n"); // s1 == s2, s1 != s2, 两个字符串相等和不等

	s1 > s3 ? printf("true\n") : printf("false\n");
	
	// 读入,到“ ”为止,例输入“I am a student!”,读入的是“I”
	// 读入自动忽略空白(空格、换行、制表符)
	cin >> s2;
	cout << "s2 from terminal: "<< s2 << endl;  
	
	// 循环读入每个单词,单词间用“ ”隔开
	string word;
	int idx = 1;
	/*while (cin >> word)
	{
		cout << "index: " << idx << " word: " << word << endl;
		idx += 1;
	}*/

	// 每次读入一行,使用getline(cin, line)函数 ,cin是输入流, line是string对象
	// getline读入是,包含尾部"\n"换行符,但是在line中没有尾部换行符
	string line;
	int line_idx = 1;
	/*while (getline(cin, line))
	{
		cout << line << endl;
	}*/

	// string  对象相加
	// 注意:“+”两侧不能同时都不是string对象类型,否则不能做加法。
	word = "world!";
	line = "hello" + word +  "world!\n";


}
           

进阶

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

/*
	string 简单操作:
		isalnum(c): 大写字母返回1,小写字母返回2,数字返回4,其他返回0
		isalpha(c): 大写字母返回1,小写字母返回2,其他返回0
		isdigit(c): 数字返回4,其他返回0
		ispunct(c): 标点符号范围16,其他返回0
		isgraph(c): 不是空格但打印时为真,
		isprint(c): 
		isspace(c): 判断是否为空格,空格范围8,其他返回0
		islower(c): 小写字母返回2,其他返回0
		isupper(c): 大写字母返回1,其他返回0
		tolower(c): 变小写字母对应的ASCII码
		toupper(c): 变大写字母对应的ASCII码
*/

void simpleStringOperation() {
	cout << "simple string operation:" << endl;
	string word;
	//cin >> word;
	getline(cin, word);

	//for (int i = 0; i < word.size(); i++) {
	//	//cout << word[i] <<" is alphabet or number? " << isalnum(word[i]) << endl;
	//	//cout << word[i] << " is alphabet? " << isalpha(word[i]) << endl;
	//	//cout << word[i] << " is digit number? " << isdigit(word[i]) << endl;
	//	//cout << word[i] << " is lower alphabet? " << islower(word[i]) << endl;
	//	//cout << word[i] << " is upper alphabet? " << isupper(word[i]) << endl;
	//	//cout << word[i] << " is punctiation? " << ispunct(word[i]) << endl;
	//	//cout << word[i] << " is graph? " << isgraph(word[i]) << endl;
	//	//cout << word[i] << " is space? " << isspace(word[i]) << endl;
	//	cout << word[i] << " is all upper alphabet? " << toupper(word[i]) << endl;
	//}	
	for (char& c : word) {
		c = toupper(c);
	}
	for (char c : word) {
		c = toupper(c);  // word不能变成大写
	}
	for (int i = 0; i < word.size(); i++) {
		word[i] = toupper(word[i]);
	}
	//cout << "typeof word: " << typeof(word) << endl;
	cout << word << endl;
}
           

继续阅读