|
string類屬性:
代碼:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1;
str1 = "hello world";
string str2("hello world");
cout << str1.capacity()<<endl; //返回當前容量(即string中不必增加內存即可存放的元素個數)
cout << str1.max_size() << endl;//返回string對象中可存放的最大字符串的長度
cout << str1.size() << endl; //返回當前字符串的大小
cout << str1.length() << endl; //返回當前字符串的長度
cout << str1.empty() << endl; //當前字符串是否爲空
str1.resize(20, '*'); //把字符串當前大小置爲20,並用字符*填充不足的部分
cout << str1.c_str() << endl;
//c_str()就是將C++的string轉化爲C的字符串數組,c_str()生成一個const char *指針,指向字符串的首地址
cout << str2 << endl;//str2是一個類 不是一個字符串
system("pause");
return 0;
}
1】
2】
函數c_str()就是將C++的string轉化爲C的字符串數組,c_str()生成一個const char *指針,指向字符串的首地址
3】
4】
|
|
string修改:
#include <iostream>
#include <string>
using namespace std;
//string類修改
void strChange()
{
string str1("abcdefg");
//---修改指定元素
cout << str1 << endl;
str1[2] = 'm';
cout << str1 << endl;
str1.at(2) = 'c';
cout << str1 << endl;
//---中間插入元素
string str2(3, 'w'); //str2="www"
str1.insert(2, str2);//在str1的第2個字符處插入str2,str1="abwwwcdefg"
cout << str1 << endl;
str1.insert(3, "zzz");//在str1的第3個字符處插入"zzz",str1="abwzzzwwcdefg"
cout << str1 << endl;
string str3 = "abcdefg";
string str4 = "hello";
str3.insert(3, str4, 3, 4);//在str3的第3個字符插入str4的第3~4個字符,str3="abclodefg"
cout << str3 << endl;
str4.insert(2, 3, '*');//在str4的第2字符處插入3個*,str4="he***llo"
cout << str4 << endl;
//---尾巴插入元素
string str5 = "abcdefg";
string str6 = "hello";
str5.insert(str5.length(), 3, '*');//在字符串str5尾巴插入3個*,str5="abcdefg***";
//str5 += "***"; //在字符串str5尾巴插入"***"
cout << str5 << endl;
str5 += str6;//將str6拼接到str5尾巴
//str5.append(str6);//將str6拼接到str5尾巴
cout << str5 << endl;
str5.append("%%%");//在字符串str5尾巴插入"%%%"
//str5.append(3,"%");//在字符串str5尾巴插入3個%
cout << str5 << endl;
//---字符串重新賦值
string str7 = "abcdefg";
string str8 = "hello";
str7 = str8;//將str8賦予str7
//str7 = "hello";//將"hello"賦予str7
//cin >> str7;//利用輸入給str7重新賦值
str7.assign("abcd", 2);//將"abcd"的前兩個字符賦值給str7
str7.assign("abcd", 2,3);//將"abcd"的後兩個字符賦值給str7
cout << str7 << endl;
//---字符串刪除
string str9 = "abcdefg";
str9.erase(2,3);//從第2個字符開始,刪除3個字符
//str9.erase(0, str9.length());//清理從開始到末尾的字符,即清空字符串
cout << str9 << endl;
}
int main()
{
strChange();
system("pause");
return 0;
}
1】
2】
3】
4】
5】
6】
|
|
字符串比較:
#include <iostream>
#include <string>
using namespace std;
//string類修改
void strCompare()
{
string str1("abcd");
string str2("abcd");
string str3("abc");
cout << (str1.compare("abcd")) << endl;//完全相等,返回0
cout << (str1 == str2) << endl;//完全相等,返回0
cout << (str1.compare("dcba")) << endl;//返回小於0
cout << (str1.compare("ab")) << endl;//返回大於0
cout << (str1.compare(0, 2, str2, 0, 2)) << endl; //str1[0-2]和str2[0-2]比較,//完全相等,返回0
cout << (str1.compare(0, 2, "abc", 0, 2)) << endl; //str1[0-2]和"abc"[0-2]比較,//完全相等,返回0
}
int main()
{
strCompare();
system("pause");
return 0;
}
1】
2】
|
|
字符串複製:
#include <iostream>
#include <string>
using namespace std;
void strCopy()
{
string str1("abcd");
string str2("***%%%");
char *s1 = new char[10];
memset(s1, 0, 10);//s1初始化爲0
//char s1[10]={0}; //或者定義一個字符數組
str1.copy(s1, 3, 1);//從str1的第一個字符開始賦值3個字符給s1
cout << s1 << endl; //s1="bcd"
delete s1;
const char* s2 = str2.data();//將string類str2轉爲字符串數組,返回給s2
str1 = str2.substr(2); //提取子串,提取出 str2 的 下標爲2 到末尾,給 str1
cout << str1 << endl;
str1 = str2.substr(2, 3); //提取子串,提取出 str2 的 下標爲2 開始,提取3個字符,給 str1
cout << str1 << endl;
}
int main()
{
strCopy();
system("pause");
return 0;
}
1】
2】
|