文章目錄
- 1.概述:
- 2.string的建構式
- 3.string的賦值操作
- 4.string的拼接操作
- 5.string的子串獲取操作
- 6.string的插入與洗掉
- 7.string的替換與查找字符操作
- 8.string的字串比較操作與尾插操作
- 9.string字符存取操作
1.概述:
string是C++風格的字串,而string本質上是一個類
string是STL的字串型別,通常用來表示字串,而在使用string之前,字串通常是用char*表示的,string與char*都可以用來表示字串,不過二者之間也有差別
1.char*是一個指標
2.string是一個類,類內部封裝了char*,管理這個字串,是一個char*型的容器
2.string的建構式
建構式原型:
1.string(); //創建一個空的字串 例如: string str;
2.string(const char* s); //使用字串s初始化
3.string(const string& str); //使用一個string物件初始化另一個string物件
4.string(int n, char c); //使用n個字符c初始化
示例:
#include<iostream>
#include<string>
using namespace std;
void test()
{
//創建空字串
string s1;//默認構造
//使用字串s初始化
const char*s = "abc";
string s2(s);
cout << "s2=" << s2<<endl;
//拷貝構造
string s3(s2);
cout << "s3=" << s3<<endl;
//用n個某字符來初始化
string s4(5, 'a');
cout << "s4=" <<s4<<endl;
}
int main()
{
test();
system("pause");
return 0;
}
3.string的賦值操作
賦值的函式原型:
1.string& operator=(const char* s); //char*型別字串 賦值給當前的字串
2.string& operator=(const string &s); //把字串s賦給當前的字串
3.string& operator=(char c); //字符賦值給當前的字串
4.string& assign(const char *s); //把字串s賦給當前的字串
5.string& assign(const char *s, int n); //把字串s的前n個字符賦給當前的字串
6.string& assign(const string &s); //把字串s賦給當前字串
7.string& assign(int n, char c); //用n個字符c賦給當前字串
示例:
#include<iostream>
#include<string>
using namespace std;
//字串賦值
void test()
{
string s1;//用的較多
s1 = "abcd";
cout <<"s1="<< s1 << endl;
string s2 = s1;
cout << "s2="<<s2 << endl;
string s3;
s3= 'a';
cout << "s3=" << s3 << endl;
string s4;
s4.assign("hello c++");
cout << "s4 = " << s4 << endl;
string s5;
s5.assign("hello world");
cout << "s4=" << s5 << endl;
string s6;
s6.assign(s5);
cout << "s6=" << s6 << endl;
string s7;
s7.assign(10,'a');
cout << "s7=" << s7 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
4.string的拼接操作
目的:實作在字串末尾拼接字串
函式原型:
1.string& operator+=(const char* str); //多載+=運算子
2.string& operator+=(const char c); //多載+=運算子
3.string& operator+=(const string& str); //多載+=運算子
4.string& append(const char *s); //把字串s連接到當前字串結尾
5.string& append(const char *s, int n); //把字串s的前n個字符連接到當前字串結尾
6.string& append(const string &s); //同operator+=(const string& str)
7.string& append(const string &s, int pos, int n);//字串s中從pos開始的n個字符連接到字串結尾
示例:
#include<iostream>
#include<string>
using namespace std;
//string字串的拼接
void test()
{
string s1 = "我";
s1 += "愛玩游戲";
cout << "s1=" << s1 << endl;
s1 += ':';
cout << "s1=" << s1 << endl;
string s2="LOL";
s1 += s2;
cout << "s1=" << s1 << endl;
string s3 = "I ";
s3.append("love ");
cout << "s3=" << s3 << endl;
s3.append("you LOL", 1);
cout << "s3=" << s3 << endl;
string s4 = "ou";
s3.append(s4);
cout << "s3=" << s3 << endl;
s3.append(s3, 7, 3);
cout << "s3=" << s3 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
運行結果:

5.string的子串獲取操作
s.substr(pos,n)
回傳一個string,包含s中從pos開始的n個字符的拷貝,pos的默認值為0
示例:
#include<iostream>
#include<string>
using namespace std;
//子串的獲取
void test()
{
string s1 = "abcd";
string substr = s1.substr(1, 3);//從s1的位置1開始獲取3個字符
cout << substr << endl;
string s2 = s1.substr(1, 5);//開始位置加計數值超過string的大小,substr會調整計數值,只拷貝到string的末尾,不會報錯
//string s = s1.substr(5, 3);
//開始位置超過了string的大小,substr函式拋出一個out_of_range 例外
cout << s2 << endl;
}
//實用操作
void test2()
{
string email = "zhangsan@qq.com";
int p = email.find('@');
string s2 = email.substr(0, p);
cout << s2 << endl;
}
int main()
{
test();
test2();
system("pause");
return 0;
}
運行結果:

6.string的插入與洗掉
插入:s.insert(pos,args)
在pos前插入args指定的字符
洗掉:s.erase(pos,len)
洗掉從位置pos開始的len個字符,如果省略len,則洗掉從pos開始直至s末尾的所有字符
示例:
#include<iostream>
#include<string>
using namespace std;
//字串的插入與洗掉
void test()
{
string s1 = "hel world";
//插入
string s2 = "hello";
s1.insert(3, "lo");//插入起始位置,插入內容
cout << s1 << endl;
//在s1[0]之前插入s2中從s2[0]開始的s2.size()個字符
s1.insert(0, s2, 0, s2.size());
cout << s1 << endl;
//洗掉
s1.erase(0, 6);//洗掉起始位置,洗掉多少個
cout << s1<<endl;
}
int main()
{
test();
system("pause");
return 0;
}
運行結果:

7.string的替換與查找字符操作
查找:s.find(args)或s.rfind(args)
s.find為從左向右查找,查找第一次args出現的位置
s.rfind為從右向左查找,查找最后一次args出現的位置
注意:查找回傳的是一個unsigned int型別,用int接收并不明智
替換:s.replace(range,args)
洗掉s中范圍range內的字符,替換為args指定的字符
replace操作是呼叫erase和insert的簡化形式
示例:
#include<iostream>
#include<string>
using namespace std;
//字串的查找
void test()
{
string s1 = "abcdefgde";
auto find = s1.find("de");//默認從0開始查找 回傳整形
if (find == -1)
{
cout << "沒找到字串" << endl;//沒找到子串回傳-1
}
else
{
cout << "find=" << find << endl;
}
//如果我們要搜索第一個不在引數中的字符,我們應該呼叫find_first_not_of
string S = "asdasfgvc23";
string S1 = "ads01234";
auto pos = S.find_first_not_of(S1);//回傳5(f)
cout << pos << endl;
//rfind從右往左查找 find 從左往右查找
auto rfind = s1.rfind("de");
cout << "rfind=" << rfind << endl;
}
//替換
void test2()
{
string s1 = "123456789";
//從第一個位置(2)替換3個字符(234)為"1111"
s1.replace(1, 3, "1111");
//等價于s1.erase(1,3);
//s1.insert(1,3,"1111");
cout << "替換后:" << s1 << endl;
}
int main()
{
test();
test2();
system("pause");
return 0;
}
運行結果:

8.string的字串比較操作與尾插操作
字串比較:s.compare(s1)
s與s1相等回傳0,s>s1回傳大于0的值,s<s1回傳一個小于0的值
尾插:s.append(args)
將args追加到s,回傳一個指向s的參考
示例:
#include<iostream>
#include<string>
using namespace std;
//字串比較
//與strcmp類似
void test()
{
//ASCII碼逐個對比
string s1 = "hello world";
string s2 = "hello world";
if (s1.compare(s2) == 0)
{
cout << "s1=s2" << endl;
}
else if (s1.compare(s2) > 0)
{
cout << "s1>s2" << endl;
}
else
{
cout << "s1<s2" << endl;
}
}
//字串尾插
void test2()
{
string s = "hello ";
string s1 = s;
s.append("world");
cout << s << endl;
//等價于
s1.insert(s1.size(), "world");
cout << s1 << endl;
}
int main()
{
test();
test2();
system("pause");
return 0;
}
運行結果:

9.string字符存取操作
string中單個字符存取方式有兩種
1.char& operator[](int n); //通過[]方式取字符
2.char& at(int n); //通過at方法獲取字符
示例:
#include<iostream>
#include<string>
using namespace std;
void test()
{
int i = 0;
string s1 = "abcd";
//用[]訪問單個字符
for (i = 0; i < s1.size(); i++)
{
cout << s1[i] << " ";
}
cout << endl;
//用at訪問
for (i = 0; i < s1.size(); i++)
{
cout << s1.at(i) << " ";
}
cout << endl;
//修改單個字符
s1[0] = 'b';
cout << s1 << endl;
s1.at(2) = 'b';
cout << s1 << endl;
}
int main()
{
test();
system("pause");
return 0;
}
運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/275038.html
標籤:其他
上一篇:C++類中的六大默認成員函式
