MFC - 字符串(String)

MFC - 字符串(String) 首页 / MFC入门教程 / MFC - 字符串(String)

字符串是代表字符序列的对象, C样式字符串起源于C语言,并在C ++中继续受支持。

  • 此字符串实际上是一维字符数组,以空字符'\0'结尾。

  • 以空字符结尾的字符串包含由该字符串组成的字符,后跟一个空字符。

这是字符数组的简单示例。

char word[12] = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '\0' };

以下是表示它的另一种方法。

char word[] = "Hello, Learnfk";

这是CString的构造函数。

Sr.No.Method & 描述
1

CString

以各种方式构造CString对象

这是数组方法的列表-

Sr.No.Method & 描述
1

GetLength

返回CString对象中的字符数。

2

IsEmpty

测试CString对象是否不包含任何字符。

3

Empty

强制字符串长度为0。

4

GetAt

返回指定位置的字符。

5

SetAt

将字符设置在指定位置。

这是比较方法的列表-

Sr.No.Method & 描述
1

Compare

比较两个字符串(区分大小写)。

2

CompareNoCase

比较两个字符串(不区分大小写)。

这是提取方法的列表-

Sr.No.Method & 描述
1

Mid

提取字符串的中间部分(如Basic MID $function)。

2

Left

提取字符串的左侧部分(如Basic LEFT $function)。

3

Right

提取字符串的右侧部分(如Basic RIGHT $function)。

4

SpanIninclude

从字符串中提取给定字符集中的字符。

5

SpanExclusion

从字符串中提取不在给定字符集中的字符。

这是转换方法的列表。

Sr.No.Method & 描述
1

MakeUpper

将此字符串中的所有字符转换为大写字符。

2

MakeLower

将此字符串中的所有字符转换为小写字符。

3

MakeReverse

反转此字符串中的字符。

链接:https://www.learnfk.comhttps://www.learnfk.com/mfc/mfc-strings.html

来源:LearnFk无涯教程网

4

Format

像sprintf一样格式化字符串。

5

TrimLeft

修剪字符串中的前导空白字符。

6

TrimRight

修剪字符串中的尾随空格字符。

这是搜索方法的列表。

Sr.No.Method & 描述
1

Find

在较大的字符串中查找字符或子字符串。

2

ReverseFind

在较大的字符串中查找字符;从头开始。

3

FindOneOf

从集合中查找第一个匹配的字符。

这里是缓冲区访问方法的列表。

Sr.No.Method & 描述
1

GetBuffer

返回指向CString中字符的指针。

无涯教程网

2

GetBufferSetLength

返回指向CString中字符的指针,将其截断为指定的长度。

3

ReleaseBuffer

释放对GetBuffer返回的缓冲区的控制

4

FreeExtra

通过释放先前分配给字符串的任何额外内存,消除此字符串对象的所有开销。

5

LockBuffer

禁用引用计数并保护缓冲区中的字符串。

6

UnlockBuffer

启用引用计数并释放缓冲区中的字符串。

这是Windows特定方法的列表。

Sr.No.Method & 描述
1

AllocSysString

从CString数据分配一个BSTR。

2

SetSysString

使用来自CString对象的数据设置现有的BSTR对象。

3

LoadString

从Windows CE资源加载现有的CString对象。

以下是对CString对象的不同操作-

创建字符串

您可以使用字符串文字或创建CString类的来创建字符串。

BOOL CMFCStringDemoDlg::OnInitDialog() {

   CDialogEx::OnInitDialog();

   //设置此对话框的图标。该框架会自动执行此操作当应用程序的主窗口不是对话框时
   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();

   //设置此对话框的图标。该框架会自动执行此操作当应用程序的主窗口不是对话框时
   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();

   //设置此对话框的图标。该框架会自动执行此操作当应用程序的主窗口不是对话框时
   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
}

编译并执行上述代码后,您将看到以下输出。

String Concatination

字符串长度

要查找字符串的长度,可以使用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
}

编译并执行上述代码后,您将看到以下输出。

弦长

字符串比较

要比较两个字符串变量,可以使用==运算符

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("Learnfk");

   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
}

编译并执行上述代码后,您将看到以下输出。

字符串比较

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

朱赟的技术管理课 -〔朱赟〕

赵成的运维体系管理课 -〔赵成〕

React实战进阶45讲 -〔王沛〕

白话法律42讲 -〔周甲徳〕

消息队列高手课 -〔李玥〕

小马哥讲Spring核心编程思想 -〔小马哥〕

Spark性能调优实战 -〔吴磊〕

陶辉的网络协议集训班02期 -〔陶辉〕

零基础入门Spark -〔吴磊〕

好记忆不如烂笔头。留下您的足迹吧 :)