(4)學習記錄 —— C++之string字符串操作

3.1 string容器

3.1.1 string基本概念

本質:

  • string是C++風格的字符串,而string本質上是一個類

string和char * 區別:

  • char * 是一個指針
  • string是一個類,類內部封裝了char*,管理這個字符串,是一個char*型的容器。

特點:

string 類內部封裝了很多成員方法

例如:查找find,拷貝copy,刪除delete 替換replace,插入insert

string管理char*所分配的內存,不用擔心複製越界和取值越界等,由類內部進行負責

3.1.2 string構造函數

構造函數原型:

  • string(); //創建一個空的字符串 例如: string str;
    string(const char* s); //使用字符串s初始化
  • string(const string& str); //使用一個string對象初始化另一個string對象
  • string(int n, char c); //使用n個字符c初始化

示例:

#include <string>
//string構造
void test01()
{
	string s1; //創建空字符串,調用無參構造函數
	cout << "str1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str); //把c_string轉換成了string

	cout << "str2 = " << s2 << endl;

	string s3(s2); //調用拷貝構造函數
	cout << "str3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "str3 = " << s3 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:string的多種構造方式沒有可比性,靈活使用即可

3.1.3 string賦值操作

功能描述:

  • 給string字符串進行賦值

賦值的函數原型:

  • string& operator=(const char* s); //char*類型字符串 賦值給當前的字符串
  • string& operator=(const string &s); //把字符串s賦給當前的字符串
  • string& operator=(char c); //字符賦值給當前的字符串
  • string& assign(const char *s); //把字符串s賦給當前的字符串
  • string& assign(const char *s, int n); //把字符串s的前n個字符賦給當前的字符串
  • string& assign(const string &s); //把字符串s賦給當前字符串
  • string& assign(int n, char c); //用n個字符c賦給當前字符串

示例:

//賦值
void test01()
{
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello c++",5);
	cout << "str5 = " << str5 << endl;


	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(5, 'x');
	cout << "str7 = " << str7 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:

​ string的賦值方式很多,operator= 這種方式是比較實用的

3.1.4 string字符串拼接

功能描述:

  • 實現在字符串末尾拼接字符串

函數原型:

  • string& operator+=(const char* str); //重載+=操作符
  • string& operator+=(const char c); //重載+=操作符
  • string& operator+=(const string& str); //重載+=操作符
  • string& append(const char *s); //把字符串s連接到當前字符串結尾
  • string& append(const char *s, int n); //把字符串s的前n個字符連接到當前字符串結尾
  • string& append(const string &s); //同operator+=(const string& str)
  • string& append(const string &s, int pos, int n);//字符串s中從pos開始的n個字符連接到字符串結尾

示例:

//字符串拼接
void test01()
{
	string str1 = "我";

	str1 += "愛玩遊戲";

	cout << "str1 = " << str1 << endl;
	
	str1 += ':';

	cout << "str1 = " << str1 << endl;

	string str2 = "LOL DNF";

	str1 += str2;

	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" love ");
	str3.append("game abcde", 4);
	//str3.append(str2);
	str3.append(str2, 4, 3); // 從下標4位置開始 ,截取3個字符,拼接到字符串末尾
	cout << "str3 = " << str3 << endl;
}
int main() {

	test01();

	system("pause");

	return 0;
}

總結:字符串拼接的重載版本很多,初學階段記住幾種即可

3.1.5 string查找和替換

功能描述:

  • 查找:查找指定字符串是否存在
  • 替換:在指定的位置替換字符串

函數原型:

  • int find(const string& str, int pos = 0) const; //查找str第一次出現位置,從pos開始查找
  • int find(const char* s, int pos = 0) const; //查找s第一次出現位置,從pos開始查找
  • int find(const char* s, int pos, int n) const; //從pos位置查找s的前n個字符第一次位置
  • int find(const char c, int pos = 0) const; //查找字符c第一次出現位置
  • int rfind(const string& str, int pos = npos) const; //查找str最後一次位置,從pos開始查找
  • int rfind(const char* s, int pos = npos) const; //查找s最後一次出現位置,從pos開始查找
  • int rfind(const char* s, int pos, int n) const; //從pos查找s的前n個字符最後一次位置
  • int rfind(const char c, int pos = 0) const; //查找字符c最後一次出現位置
  • string& replace(int pos, int n, const string& str); //替換從pos開始n個字符爲字符串str
  • string& replace(int pos, int n,const char* s); //替換從pos開始的n個字符爲字符串s

示例:

//查找和替換
void test01()
{
	//查找
	string str1 = "abcdefgde";

	int pos = str1.find("de");

	if (pos == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "pos = " << pos << endl;
	}
	

	pos = str1.rfind("de");

	cout << "pos = " << pos << endl;

}

void test02()
{
	//替換
	string str1 = "abcdefgde";
	str1.replace(1, 3, "1111");

	cout << "str1 = " << str1 << endl;
}

int main() {

	//test01();
	//test02();

	system("pause");

	return 0;
}

總結:

  • find查找是從左往後,rfind從右往左
  • find找到字符串後返回查找的第一個字符位置,找不到返回-1
  • replace在替換時,要指定從哪個位置起,多少個字符,替換成什麼樣的字符串

3.1.6 string字符串比較

功能描述:

  • 字符串之間的比較

比較方式:

  • 字符串比較是按字符的ASCII碼進行對比

= 返回 0

> 返回 1

< 返回 -1

函數原型:

  • int compare(const string &s) const; //與字符串s比較
  • int compare(const char *s) const; //與字符串s比較

示例:

//字符串比較
void test01()
{

	string s1 = "hello";
	string s2 = "aello";

	int ret = s1.compare(s2);

	if (ret == 0) {
		cout << "s1 等於 s2" << endl;
	}
	else if (ret > 0)
	{
		cout << "s1 大於 s2" << endl;
	}
	else
	{
		cout << "s1 小於 s2" << endl;
	}

}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:字符串對比主要是用於比較兩個字符串是否相等,判斷誰大誰小的意義並不是很大

3.1.7 string字符存取

string中單個字符存取方式有兩種

  • char& operator[](int n); //通過[]方式取字符
  • char& at(int n); //通過at方法獲取字符

示例:

void test01()
{
	string str = "hello world";

	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;


	//字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
	
}

int main() {

	test01();

	system("pause");

	return 0;
}

總結:string字符串中單個字符存取有兩種方式,利用 [ ] 或 at

3.1.8 string插入和刪除

功能描述:

  • 對string字符串進行插入和刪除字符操作

函數原型:

  • string& insert(int pos, const char* s); //插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c); //在指定位置插入n個字符c
  • string& erase(int pos, int n = npos); //刪除從Pos開始的n個字符

示例:

//字符串插入和刪除
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);  //從1號位置開始3個字符
	cout << str << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

**總結:**插入和刪除的起始下標都是從0開始

3.1.9 string子串

功能描述:

  • 從字符串中獲取想要的子串

函數原型:

  • string substr(int pos = 0, int n = npos) const; //返回由pos開始的n個字符組成的字符串

示例:

//子串
void test01()
{

	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "[email protected]";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;

}

int main() {

	test01();

	system("pause");

	return 0;
}

**總結:**靈活的運用求子串功能,可以在實際開發中獲取有效的信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章