一、C++ CString 函數
CString 函數是 C++ 標準庫中一個非常重要的函數庫,它提供了各種與字元串操作相關的函數。在 C++ 代碼中,往往需要對字元串進行處理,比如獲取字元串長度、複製或連接字元串、查找字元串中的字元和子字元串等。這些操作都可以通過 CString 函數來實現。其中一些常用的函數如下:
strlen():用於獲取字元串長度。strcpy():用於將一個字元串複製到另外一個字元串中。strcat():用於將一個字元串連接到另外一個字元串尾部。strchr():用於查找字元在字元串中的位置。strstr():用於查找子字元串在字元串中的位置。
下面給出一個代碼示例:
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
char str1[] = "Hello", str2[] = "World";
char buffer[50];
cout << "strlen(str1) = " << strlen(str1) << endl;
strcpy(buffer, str1);
strcat(buffer, str2);
cout << "strcat(buffer, str2) = " << buffer << endl;
cout << "strchr(str1, 'e') = " << strchr(str1, 'e') - str1 + 1 << endl;
cout << "strstr(buffer, \"World\") = " << strstr(buffer, "World") - buffer + 1 << endl;
return 0;
}
二、C CString 循環
對於一個字元串,往往需要遍歷它的每一個字元來進行操作。CString 對象有很多迭代器和循環方法用來遍歷它所有的字元,如下所示:
GetLength():獲取字元串的長度。GetAt():獲取字元串中指定位置的字元。SetAt():設置字元串中指定位置的字元。IsEmpty():檢查字元串是否為空。MakeLower():將字元串中所有的字元轉換成小寫。MakeUpper():將字元串中所有的字元轉換成大寫。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str1("Hello");
CString str2(" World");
for(int i=0; i<str1.GetLength(); i++) {
cout << "str1[" << i << "] = " << str1.GetAt(i) << endl;
}
str1.SetAt(1, 'a');
cout << "str1 = " << str1 << endl;
cout << "str1.IsEmpty() = " << str1.IsEmpty() << endl;
cout << "str1.MakeLower() = " << str1.MakeLower() << endl;
str2.MakeUpper();
cout << "str2 = " << str2 << endl;
return 0;
}
三、C CString 字元串分割
字元串分割是一個常用的操作。對於一個字元串,根據指定標識來分割它可以得到一個分割後的字元串數組。CString 提供了 Split 函數用於實現字元串分割操作。其中,第一個參數是分割後的字元串數組,第二個參數是分隔符,第三個參數是一個布爾值,用於指定是否要刪除空字元串。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str1("Hello, world, I, am, here");
CStringArray arr;
str1.Split(",", arr);
for(int i=0; i<arr.GetSize(); i++) {
cout << "arr[" << i << "] = " << arr.GetAt(i) << endl;
}
return 0;
}
四、C CString 頭文件
CString 的定義在頭文件 atlstr.h 中。為了能夠正確使用 CString,需要在程序中包含該頭文件。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str("Hello, world!");
cout << "str = " << str << endl;
return 0;
}
五、C CString Find
CString Find 函數用於查找字元串中指定字元或字元子串的位置,如果找到了,則返回該字元或字元子串在字元串中的位置。如果沒有找到,則返回 -1。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str("Hello, world!");
cout << "str.Find(\"world\") = " << str.Find("world") << endl;
return 0;
}
六、C CString 中是否包含某個字元串
CString 類提供了一個函數 Find,該函數用於查找一個子串在另一個字元串中的位置。可以根據此函數判斷一個字元串是否包含另一個字元串。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str("Hello, world!");
if(str.Find("world") != -1) {
cout << "str contains \"world\"" << endl;
}
return 0;
}
七、C CString 類
CString 類是一個字元串處理類,封裝了很多與字元串操作有關的函數,相比 C-Style 字元串的操作更加簡單,易於使用。通過 CString 類,可以方便地進行字元串的拼接、分割、複製等操作,而不必關心內存的分配和釋放問題。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str1("Hello"), str2("World");
CString str3 = str1 + str2;
cout << "str3 = " << str3 << endl;
return 0;
}
八、C CString 轉 const char*
CString 類中提供了一個函數 GetBuffer,用於將 CString 類型轉換為 C-Style 字元串類型。這個函數返回一個 const char* 類型的指針,指向 CString 內部的 char 型數組。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
using namespace std;
int main()
{
CString str("Hello, World!");
const char* p = str.GetBuffer(0);
cout << "p = " << p << endl;
return 0;
}
九、C CString 轉 string
CString 類中提供了一個函數 GetString,用於將 CString 類型轉換為標準字元串類型 std::string。需要注意的是,為了減少內存的分配和釋放次數,在調用該函數之後,需要手動釋放 CString 對象內部分配的內存。
下面給出一個代碼示例:
#include <atlstr.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
CString str("Hello, World!");
string s = CT2A(str);
str.ReleaseBuffer();
cout << "s = " << s << endl;
return 0;
}
原創文章,作者:KVSO,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/136941.html
微信掃一掃
支付寶掃一掃