#include

1 append(string T&);字符串拼接ios

2 c_str數組

string.c_str是Borland封裝的String類中的一個函數,它返回當前字符串的首字符地址。app

3 empty();判斷是否爲空函數

4 erasespa

刪除code

5 findblog

在字符串中查找Find content in string字符串

6 find_first_not_ofstring

Find absence of character in stringit

7 find_first_of

Find character in string

8 find_last_not_of

Find non-matching character in string from the end

9 find_last_of

Find character in string from the end

10 rfind

Find last occurrence of content in string

11 insert

插入

12 replace

替換

 

打印字符串,並執行

system(string1[i].c_str());

 

//std::array<std::string, 數組元素個數>

 

 1 #include <iostream>
 2 #include <array>
 3 #include <string>
 4 using namespace std;
 5 
 6 void main()
 7 {
 8     std::array<std::string, 5> string1 = { "calc","notpad","tasklist","mspaint","write" };//std::array<std::string, 數組元素個數>
 9 
10     for (int i = 0; i < 5; i++)
11     {
12         std::cout << string1[i] << std::endl;//打印
13         system(string1[i].c_str());//執行
14     }
15 
16     system("pause");
17 };

 

字符串相加,相似C的strcat函數

 

 1 #include <iostream>
 2 #include <array>
 3 #include <string>
 4 using namespace std;
 5 
 6 void main()
 7 {
 8     std::string str1 = "task";
 9     std::string str2 = "list";
10     std::string str3 = str1 + str2;
11 
12     system(str3.c_str());
13 
14     system("pause");
15 }

 

C: char str[]

C++: string

注意區分二者不一樣,string是類

error C3863: 不可指定數組類型「char [100]」

 

 1 #include <iostream>
 2 
 3 void main()
 4 {
 5     char str[100];
 6     str = "hello";//error C3863: 不可指定數組類型「char [100]」
 7 
 8     std::string str1;
 9     str1 = "world";
10 }

 

append能夠實現字符串拼接

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str1("hello");
 7     std::string str2("world");;
 8 
 9     str1.append(str2);//字符串拼接
10 
11     std::cout << str1 << std::endl;
12 }

 

str1.insert(str1.begin(), 'X');//頭部插入

str1.insert(str1.end(), 'X');//尾部插入

str1.insert(str1.begin(), 3, 'X');//頭部插入3個X

str1.insert(str1.begin() + 3, 3, 'X');//頭部插入3個X,在開頭第3個位置插入

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str1("hello");
 7     std::string str2("world");;
 8 
 9     str1.insert(str1.begin(), 'X');//頭部插入
10     str1.insert(str1.end(), 'X');//尾部插入
11 
12     str1.insert(str1.begin(), 3, 'X');//頭部插入3個X
13 
14     str1.insert(str1.begin() + 3, 3, 'X');//頭部插入3個X,在開頭第3個位置插入
15 
16     std::cout << str1 << std::endl;
17 }

 

str.erase(str.begin());//刪除第一個字符
str.erase(3, 4);//從下標3開始,刪除4個字符(下標從0開始)

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str("helloworld");
 7 
 8     str.erase(str.begin());//刪除第一個字符
 9     str.erase(3, 4);//從下標3開始,刪除4個字符(下標從0開始)
10 
11     std::cout << str << std::endl;
12 }

 

str.replace(0, 3, "china");//從下標0開始,把3個字符替換成china
//位置,長度,字符串

str.replace(0, 3, "china");//從下標0開始,直接插入china

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str("helloworld");
 7 
 8     str.replace(0, 3, "china");//從下標0開始,把3個字符替換成china
 9     //位置,長度,字符串
10 
11     str.replace(0, 3, "china");//從下標0開始,直接插入china
12 
13     std::cout << str << std::endl;
14 }

 

各類find

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str("helloworld");
 7 
 8     std::cout << str.find("wo") << std::endl;//正向查找
 9 
10     std::cout << str.rfind("wo") << std::endl;//反向查找
11 
12     std::cout << str.find_first_of("wo") << std::endl;//最後一個找到與字符串匹配的字符位置
13     
14     std::cout << str.find_first_not_of("wo") << std::endl;
15 
16     std::cout << str.find_last_of("wo") << std::endl;
17 
18     std::cout << str.find_last_not_of("wo") << std::endl;
19 
20     std::cout << str << std::endl;
21 }

 

string判斷是否同樣

 

 1 #include <iostream>
 2 #include <string>
 3 
 4 void main()
 5 {
 6     std::string str1("helloworld");
 7     std::string str2("helloworld");
 8 
 9     char str3[100] = "helloworld";
10     char str4[100] = "helloworld";
11     
12     std::cout << (str1 == str2) << std::endl;//string重載==
13     std::cout << (str3 == str4) << std::endl;//判斷地址是否同樣,地址確定不同,永遠是0
14 
15     std::cout << str1.empty() << std::endl;//判斷是否爲空
16 }

 

 

 

 

 

121456

相關文章
相關標籤/搜索