📜  MFC-字符串

📅  最后修改于: 2020-11-20 06:33:04             🧑  作者: Mango


字符串是代表字符序列的对象。 C风格的起源于C语言中,并继续进行C++中支持。

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

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

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

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. Method & Description
1

CString

Constructs CString objects in various ways

这是数组方法的列表-

Sr.No. Method & Description
1

GetLength

Returns the number of characters in a CString object.

2

IsEmpty

Tests whether a CString object contains no characters.

3

Empty

Forces a string to have 0 length.

4

GetAt

Returns the character at a specified position.

5

SetAt

Sets a character at a specified position.

这是比较方法的列表-

Sr.No. Method & Description
1

Compare

Compares two strings (case sensitive).

2

CompareNoCase

Compares two strings (case insensitive).

这是提取方法的列表-

Sr.No. Method & Description
1

Mid

Extracts the middle part of a string (like the Basic MID$ function).

2

Left

Extracts the left part of a string (like the Basic LEFT$ function).

3

Right

Extracts the right part of a string (like the Basic RIGHT$ function).

4

SpanIncluding

Extracts the characters from the string, which are in the given character set.

5

SpanExcluding

Extracts the characters from the string which are not in the given character set.

这是转换方法的列表。

Sr.No. Method & Description
1

MakeUpper

Converts all the characters in this string to uppercase characters.

2

MakeLower

Converts all the characters in this string to lowercase characters.

3

MakeReverse

Reverses the characters in this string.

4

Format

Format the string as sprintf does.

5

TrimLeft

Trim leading white-space characters from the string.

6

TrimRight

Trim trailing white-space characters from the string.

这是搜索方法的列表。

Sr.No. Method & Description
1

Find

Finds a character or substring inside a larger string.

2

ReverseFind

Finds a character inside a larger string; starts from the end.

3

FindOneOf

Finds the first matching character from a set.

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

Sr.No. Method & Description
1

GetBuffer

Returns a pointer to the characters in the CString.

2

GetBufferSetLength

Returns a pointer to the characters in the CString, truncating to the specified length.

3

ReleaseBuffer

Releases control of the buffer returned by GetBuffer

4

FreeExtra

Removes any overhead of this string object by freeing any extra memory previously allocated to the string.

5

LockBuffer

Disables reference counting and protects the string in the buffer.

6

UnlockBuffer

Enables reference counting and releases the string in the buffer.

这是Windows特定方法的列表。

Sr.No. Method & Description
1

AllocSysString

Allocates a BSTR from CString data.

2

SetSysString

Sets an existing BSTR object with data from a CString object.

3

LoadString

Loads an existing CString object from a Windows CE resource.

以下是对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
}

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

弦长

字符串比较

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

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
}

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

字符串比较