C++字串操作
String
Strings are sequences of characters(char)
練習: a130
Construct
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "Initial string";
string s1 ("Initial string");
return 0;
}
字串的宣告以及初始化
上面兩個都會將字串初始化為"Initial string"
Construct
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "123";
string s1 = s;
string s2 (s);
return 0;
}
7, 8行也是等價的,都是將字串s賦予給s1及s2
Operator
讀取特定位置的值
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s ("Test string");
cout << s[2] <<"\n";
cout << s[5] <<"\n";
return 0;
}
字串其實可以想成是由字元(char)所構成的一個陣列
因此你可以使用像是陣列的index,去存取或修改string
修改特定位置的值
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s ("Test string");
s[2] = '3'
cout << s <<"\n";
return 0;
}
Remark: 是操作的 = 後面是加上 ' '
陣列的index是從0開始算
連接字串
int main()
{
string s = "abc", b = "ddd";
s += "d";
b = b + s;
cout << s <<"\n";
cout << b <<"\n";
return 0;
}
用 + 可以將左右兩個字串聯接再一起,常用來新增字串
Q:如何製造一個由'*'組成長度為30的字串
string s (30, '*');
string s("alplpllle");
cout << s.size() << "\n";
cout << s.length() << "\n";
for(int i = 0; i < s.size(); i += 2)
cout << s[i];字串長度
使用size()和length()都可以拿到字串的長度
很常在需要遍歷字串時搭配迴圈使用
Modifiers:
擷取部分字串
string s = "I don't like C++ at all";
cout << s.substr(8, 4);拿到substring,第一個參數是position(pos),第二個是length
想要從字串"I don't like C++ at all" 擷取like
string s = "I don't like C++ at all";
string s2;
for(int i = 8; i < 8 + 4; i++)
s2 += s[i];
cout << s2 << endl; // Output: "like"在字串中插入字串
string s("ale");
s.insert(1, "pp");
cout << s <<"\n";
今天有一個字串("ale"),如何在位置1插入'pp'把字串變成"apple"
第一個參數是插入位置,第二個是要插入的字串
刪除部分字串
string s("This is an example sentence.");
s.erase (10,8);
cout << s << '\n';
string s("This is an example sentence.");
// "This is an example sentence."
// ^^^^^^^^
string s2;
for(int i = 0; i < 10; i++)
s2 += s[i];
for(int i = 18; i < s.size(); i++)
s2 += s[i];
s = s2;
cout << s << '\n';
第一個參數是pos,第二個參數是len
Replace
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="I is hungry";
str.replace(2, 2, "am");
cout << str <<"\n";
return 0;
}第一個參數pos是替換位置
第二個參數len替換長度
第三個位置是要替換的字串
String to Int (stoi)
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s = "123";
int a = stoi(s);
cout << a <<"\n";
return 0;
}
將string的type 從string轉成int
to_string() 數字轉字串
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a = 5487;
string s = to_string(a);
s += "9";
cout << s <<"\n";
return 0;
}
Getline
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
cout << "Please, enter your full name: ";
getline (cin,name);
cout << "Hello, " << name << "!\n";
return 0;
}
cin 會在空白或換行時斷開,但getline可以擷取一整行並保留空白
練習:a011
Assign
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
string base="The quick brown ";
str.assign(base);
str.assign(base,4,5);
str.assign("pangrams are cool",7);
str.assign("c-string");
str.assign(10,'*');
return 0;
}ASCII
將 128 個字元(0–127)對應到整數代碼的標準編碼系統
將每個字元(char)編碼,並對應到一個整數

Char
#include <iostream>
using namespace std;
int main() {
cout << "ASCII of 'A' is "
<< int('A') << "\n"; // 65
cout << "ASCII of 'a' is "
<< int('a') << "\n"; // 97
cout << "ASCII of '\\n' is " //跳脫字元
<< int('\n') << "\n"; // 10
cout << char(65);
return 0;
}
char本身會對應到一個數字
<ctype.h>
char c = '1';
if(isdigit(c))
cout << "c is digit.\n";
isdigit: 檢查是否為數字
isalpha: 檢查是否為字母
isalnum: 檢查是否為數字或字母
islower: 檢查是否為小寫
isupper: 檢查是否為大寫
isblank: 檢查是否為空白
常用
#include <iostream>
using namespace std;
int main()
{
//單個位元轉換成數字
char c = '1';
int a = c - '0';
cout << a <<"\n";
return 0;
}
既然對應到數字,應該也可以用+-來操作
計算char和'0'的差,可以把char變成int
常用
#include <iostream>
using namespace std;
int main()
{
//小寫轉成大寫
char s = 'd';
s += 'A' - 'a';
cout << s <<"\n";
return 0;
}
既然對應到數字,應該也可以用+-來操作
把s加上大寫和小寫的差距,可以把s變成大寫
string s = "ABC";
for(int i = 0; i < s.size(); i++)
s[i] = tolower(s[i]);
cout << s <<"\n";
Similar to toupper
- tolower可以把大寫變成小寫
- toupper可以把小寫變成大寫
- a009. 解碼器
- b428. 凱薩加密
- d235: 10929 – You can say 11
C++字串操作
By ernestii26
C++字串操作
- 30