【C++ 筆記】字串(String) - part 9

很感謝你點進來這篇文章。

你好,我並不是什麼 C++、程式語言的專家,所以本文若有些錯誤麻煩請各位鞭大力一點,我極需各位的指正及指導!!本系列文章的性質主要以詼諧的口吻,一派輕鬆的態度自學程式語言,如果你喜歡,麻煩留言說聲文章讚讚吧!

字串(String)

字串是由許多的字元所組成的,但字串實際上它是一個一維陣列,以 null 字元(’\0’)終止符號為結尾。

如下:

1
char a[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

Hello 共有 5 個字元,但實際上在 C++ 中存在於 6 個字元,因多一個 ‘\0’ 表示字串的結尾。

arraysize 因此要寫 6。

也可以寫成這樣:

1
char a[] = "Hello";

但實際上我們也不用那麼麻煩,加上 ‘\0’ 上去,C++ 編譯器會在初始化陣列時自動加上去。

以下是一個範例,輸出以上的說明文字:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main(){
char a[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char b[] = "Hello";
cout << a << endl;
cout << b;
return 0;
}

輸出結果:

1
2
Hello
Hello

可發現 ‘\0’ 沒有在上面,因為 null 字元或是 ‘\0’ 本身就是一個”空”的字元,表示一個字串的結尾。

cstring 函式庫

語法:

1
#include <cstring>
序號函數
1strcpy(s1, s2); -> 複製字串 s2 至 s1
2strcat(s1, s2); -> 連接字串 s2 到字串 s1 的結尾,連接字串也可以用 + 號。
3strlen(s1); -> 回傳字串 s1 的長度。
4strcmp(s1, s2); -> 如果 s1 和 s2 是相同的,則回傳 0;如果 s1 < s2 則回傳值小於 0;如果 s1>s2 則傳回值大於 0。
5strchr(s1, ch); -> 回傳一個指標,指向字串 s1 中字元 ch 第一次出現的位置。
6strstr(s1, s2); -> 回傳一個指標,指向字串 s1 中字串 s2 第一次出現的位置。

細則可參考:https://www.w3schools.com/cpp/cpp_ref_cstring.asp

以下是一範例,可當作練習:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstring> // C 字串處理函式庫
using namespace std;

int main() {
char s1[100] = "Hello, ";
char s2[] = "World!";
char s3[100];
char ch = 'o';

// 1. strcpy - 複製字串
strcpy(s3, s2);
cout << "用 strcpy 將 s2 複製到 s3: " << s3 << endl;

// 2. strcat - 字串連接
strcat(s1, s2);
cout << "用 strcat 將 s2 連接到 s1: " << s1 << endl;

// 3. strlen - 獲取字串長度
cout << "s1 的長度: " << strlen(s1) << endl;

// 4. strcmp - 比較字串
int cmpResult = strcmp(s1, s2);
if (cmpResult == 0) {
cout << "s1 = s2" << endl;
} else if (cmpResult < 0) {
cout << "s1 < s2" << endl;
} else {
cout << "s1 > s2" << endl;
}

// 5. strchr - 查找字元在字串中的位置
char* chPos = strchr(s1, ch);
if (chPos) {
cout << "字元 '" << ch << "' 第一次出現在 s1 的位置: "
<< (chPos - s1) << endl;
} else {
cout << "字元 '" << ch << "' 未在 s1 中找到" << endl;
}

// 6. strstr - 查找子字串在字串中的位置
char* subStrPos = strstr(s1, s2);
if (subStrPos) {
cout << "字串 \"" << s2 << "\" 第一次出現在 s1 的位置: "
<< (subStrPos - s1) << endl;
} else {
cout << "字串 \"" << s2 << "\" 未在 s1 中找到" << endl;
}

return 0;
}

輸出結果:

1
2
3
4
5
6
用 strcpy 將 s2 複製到 s3: World!
用 strcat 將 s2 連接到 s1: Hello, World!
s1 的長度: 13
s1 > s2
字元 'o' 第一次出現在 s1 的位置: 4
字串 "World!" 第一次出現在 s1 的位置: 7

string 函式庫

語法:

1
#include <string>

string 是一個類別(Class),底部有許多方法(Method)可使用,這要等到學物件導向程式設計(OOP)才會開始接觸之。

可能要到後期才會介紹到此函式庫。

以下是一個範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>

using namespace std;

int main ()
{
string str1 = "runoob";
string str2 = "google";
string str3;
int len ;

// 複製 str1 到 str3
str3 = str1;
cout << "str3 : " << str3 << endl;

// 連接 str1 和 str2
str3 = str1 + str2;
cout << "str1 + str2 : " << str3 << endl;

// 連接後,str3 的總長度
len = str3.size();
cout << "str3.size() : " << len << endl;

return 0;
}

輸出結果:

1
2
3
str3 : runoob
str1 + str2 : runoobgoogle
str3.size() : 12

來源:https://www.runoob.com/cplusplus/cpp-strings.html

總結

本文介紹 C++ 中的字串(String)處理,包括基本概念、C 字串和 C++ 字串的使用。

字串的基本概念

字串是由多個字元組成的一維陣列,並以 null 字元(’\0’)作為結尾。以下範例中為定義字串:

1
2
char a[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char b[] = "Hello"; // 編譯器自動加上 '\0'

C 標準函式庫 cstring

本文列出了幾個常用的 C 字串處理函數:

strcpy(s1, s2):複製字串 s2 至 s1。
strcat(s1, s2):將字串 s2 連接到 s1 的結尾。
strlen(s1):返回字串 s1 的長度。
strcmp(s1, s2):比較兩個字串。
strchr(s1, ch):查找字元 ch 在 s1 中第一次出現的位置。
strstr(s1, s2):查找字串 s2 在 s1 中第一次出現的位置。

參考資料

C++ cstring Library Reference (cstring functions)

Strings in C++ - GeeksforGeeks

C++ 字符串 | 菜鸟教程