MFC – 字符串
字符串是表示字符序列的对象. C风格的字符串起源于C语言,并继续在C ++中得到支持.
-
这个字符串实际上是一个一维字符数组,以空字符"\0"结尾.
-
以null结尾的字符串包含构成字符串的字符by null.
以下是字符数组的简单示例.
char word[12] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0' };
以下是表示它的另一种方式.
char word[] = "Hello, World";
Microsoft基础类(MFC)库提供了一个类来操作名为 CString 的字符串.以下是CString的一些重要功能.
-
CString没有基类.
-
CString对象由可变长度的字符序列组成.
-
CString使用类似的语法提供函数和运算符对于Basic.
-
连接和比较运算符以及简化的内存管理使CString对象比普通字符数组更容易使用.
这是CString的构造函数.
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
CString 以各种方式构造CString对象 |
这是一个数组方法列表 :
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
GetLength 返回CString对象中的字符数. |
| 2 |
IsEmpty 测试一个CString对象不包含任何字符. |
| 3 |
Empty 强制字符串长度为0. |
| 4 |
GetAt 返回指定位置的字符. |
| 5 |
SetAt 在指定位置设置一个字符. |
以下是比较方法列表 :
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
Compare 比较两个字符串(区分大小写). |
| 2 |
CompareNoCase 比较两个字符串(不区分大小写) . |
以下是提取方法列表 :
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
Mid 提取字符串的中间部分(如Basic MID $函数). |
| 2 |
Left 提取字符串的左侧部分(如Basic LEFT $函数). |
| 3 |
Right 提取字符串的右侧部分(如Basic RIGHT $函数) ). |
| 4 |
SpanIncluding 从字符串中提取给定字符集中的字符. |
| 5 |
SpanExcluding 从字符串中提取不在给定字符集中的字符. |
以下是转换方法列表.
| Sr.No. | Method&说明 |
|---|---|
| 1 |
MakeUpper 将此字符串中的所有字符转换为大写字符. |
| 2 |
MakeLower 转换所有字符在此字符串中为小写字符. |
| 3 |
MakeReverse 反转此字符串中的字符. |
| 4 |
Format 将字符串格式化为sprintf. |
| 5 |
TrimLeft 修剪字符串中的前导空格字符. |
| 6 |
TrimRight 修剪字符串中的尾随空白字符. |
这是搜索方法列表.
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
Find 在较大的字符串中查找字符或子字符串. |
| 2 |
ReverseFind 查找内部角色大串;从最后开始. |
| 3 |
FindOneOf 查找集合中的第一个匹配字符. |
以下是缓冲区访问方法列表.
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
GetBuffer 返回指向CString中字符的指针. |
| 2 |
GetBufferSetLength 返回指向CString中的字符,截断为指定的长度. |
| 3 |
ReleaseBuffer 释放对GetBuffer返回的缓冲区的控制 |
| 4 |
FreeExtra 通过释放先前分配给字符串的任何额外内存来消除此字符串对象的任何开销. |
| 5 |
LockBuffer 禁用引用计数并保护缓冲区中的字符串. |
| 6 |
UnlockBuffer 启用引用计数并释放缓冲区中的字符串. |
以下是特定于Windows的方法列表.
| Sr.No. | 方法&说明 |
|---|---|
| 1 |
AllocSysString 从CString数据中分配BSTR. |
| 2 |
SetSysString 使用来自的数据设置现有BSTR对象一个CString对象. |
| 3 |
LoadString 从Windows CE资源加载现有的CString对象. |
以下是对CString对象的不同操作 :
创建字符串
你可以通过使用字符串文字或创建CString类的实例来创建字符串.
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CString string1 = _T("This is a string1");
CString string2("This is a string2");
m_strText.Append(string1 + L"\n");
m_strText.Append(string2);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
编译并执行上述代码时,您将看到以下输出.

空字符串
您可以使用空字符串文字或使用CString :: Empty()方法创建空字符串.您还可以使用布尔属性isEmpty检查字符串是否为空.
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CString string1 = _T("");
CString string2;
string2.Empty();
if(string1.IsEmpty())
m_strText.Append(L"String1 is empty\n");
else
m_strText.Append(string1 + L"\n");
if(string2.IsEmpty())
m_strText.Append(L"String2 is empty");
else
m_strText.Append(string2);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
编译并执行上述代码时,您将看到以下输出.

字符串连接
要连接两个或多个字符串,可以使用+运算符连接两个字符串或CString :: Append()方法.
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//To concatenate two CString objects
CString s1 = _T("This "); // Cascading concatenation
s1 += _T("is a ");
CString s2 = _T("test");
CString message = s1;
message.Append(_T("big ") + s2);
// Message contains "This is a big test".
m_strText = L"message: " + message;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
编译并执行上述代码时,您将看到以下输出.

字符串长度
要查找字符串的长度,可以使用CString :: GetLength()方法,该方法返回CString对象中的字符数.
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CString string1 = _T("This is string 1");
int length = string1.GetLength();
CString strLen;
strLen.Format(L"\nString1 contains %d characters", length);
m_strText = string1 + strLen;
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
编译并执行上述代码时,您将看到以下输出.

字符串比较
要比较两个字符串变量,你可以使用== operator
BOOL CMFCStringDemoDlg::OnInitDialog() {
CDialogEx::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CString string1 = _T("Hello");
CString string2 = _T("World");
CString string3 = _T("MFC Tutorial");
CString string4 = _T("MFC Tutorial");
if (string1 == string2)
m_strText = "string1 and string1 are same\n";
else
m_strText = "string1 and string1 are not same\n";
if (string3 == string4)
m_strText += "string3 and string4 are same";
else
m_strText += "string3 and string4 are not same";
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
编译并执行上述代码时,您将看到以下输出.
