MFC - Strings


Advertisements

Strings are objects that represent sequences of characters. The C-style character string originated within the C language and continues to be supported within C++.

  • This string is actually a one-dimensional array of characters which is terminated by a null character '\0'.

  • A null-terminated string contains the characters that comprise the string followed by a null.

Here is the simple example of character array.

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

Following is another way to represent it.

char word[] = "Hello, World";

Microsoft Foundation Class (MFC) library provides a class to manipulate string called CString. Following are some important features of CString.

  • CString does not have a base class.

  • A CString object consists of a variable-length sequence of characters.

  • CString provides functions and operators using a syntax similar to that of Basic.

  • Concatenation and comparison operators, together with simplified memory management, make CString objects easier to use than ordinary character arrays.

Here is the constructor of CString.

Sr.No. Method & Description
1

CString

Constructs CString objects in various ways

Here is a list of Array Methods −

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.

Here is a list of Comparison Methods −

Sr.No. Method & Description
1

Compare

Compares two strings (case sensitive).

2

CompareNoCase

Compares two strings (case insensitive).

Here is a list of Extraction Methods −

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.

Here is a list of Conversion Methods.

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.

Here is a list of Searching Methods.

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.

Here is a list of Buffer Access Methods.

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.

Here is a list of Windows-Specific Methods.

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.

Following are the different operations on CString objects −

Create String

You can create a string by either using a string literal or creating an instance of CString class.

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
}

When the above code is compiled and executed, you will see the following output.

Create String

Empty String

You can create an empty string by either using an empty string literal or by using CString::Empty() method. You can also check whether a string is empty or not using Boolean property 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
}

When the above code is compiled and executed you will see the following output.

Empty String

String Concatenation

To concatenate two or more strings, you can use + operator to concatenate two strings or a CString::Append() method.

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
}

When the above code is compiled and executed you will see the following output.

String Concatination

String Length

To find the length of the string you can use the CString::GetLength() method, which returns the number of characters in a CString object.

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
}

When the above code is compiled and executed you will see the following output.

String Length

String Comparison

To compare two strings variables you can use == 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
}

When the above code is compiled and executed you will see the following output.

String Comparison
Advertisements