This article mainly summarizes some knowledge related to MFC programming string under Windows, such as the use of CString, CStringList, etc
CString
The best string class used in MFC should be CString. CString is a class in MFC, which contains many easy-to-use operations, such as Format, search, calculate length, etc
To use CString, you need to reference the header file in the project: #include < AFX h> , usually placed in StdAfx H in the precompiled header In addition, you need to select "use MFC in shared DLL" in the project properties
There were previous projects in VC6 0. Later, it was migrated to VS2013. At the beginning, a large number of errors were reported about CString, and it was found that it was characters from different platforms For coding problems, Download Multibyte MFC Library for Visual Studio 2013 from the Internet. After installation, select Multi byte encoding instead of Unicode, that is, there is no encoding problem, and CString can be used freely
// use_CString.c // initialization CString s; CString s("hello"); CString s = "hello"; char c[] = "hello"; CString s = ""; s.Format("%s", c); CString s = "hello"; // length // Note: each character in English takes up one length, and each character in Chinese takes up two lengths printf("%d", s.GetLength()); // 5 // reversal s.MakeReverse(); // "olleh" // Convert case s.MakeUpper(); // "HELLO" s.MakeLower(); // "hello" // Insert delete s.Insert(2, "a"); // "heallo" s.Delete(3, 2); // "hel" // Replace and remove specified characters s.Replace("ll", "yy"); // "heyyo" s.Remove('l'); // "heo" // Remove left and right spaces // Generally, when reading a string from a file, the spaces at both ends will be removed first to prevent reading meaningless data s.TrimLeft(); // The left space is removed by default s.TrimRight("a"); // Remove any number of "a" at the right end // Clear the string and judge whether the string is empty // Judging whether it is empty is also commonly used to read files s.Empty(); s.IsEmpty(); // Returns 0 when null // lookup s.Find('e'); // 1 s.Find('ll'); // 2 s.Find('e', 1); // 0 s.Find('a'); // Return - 1 not found s.ReverseFind('e'); // Reverse search, that is, reverse first and then find, 3 // format s.Format("%d", 2); // "2" // Value taking and assignment s.GetAt(2); // "l" If the index is out of bounds, an exception will occur s.SetAt(2, 'h'); // "hehlo" s.Left(2); // "he" s.Right(2); // "lo" s.Mid(2, 2); // "ll" s.Mid(2); // "llo"
CString interconverts with other types
CString is commonly used in MFC with high security but poor portability string is commonly used in STL char * is commonly used for input parameters of API
// convert_CString.c // 1 CString and char* CString s = "hello"; char *p = (LPSTR)(LPCTSTR)s; char p[] = "world"; s.Format("%s", p); // 2 CString and string CString s; string str = "hello"; s.Format("%s", str.c_str()); CString s = "hello"; string str(s.GetBuffer()); // string type cannot be printed with printf CString.ReleaseBuffer(); // 3 char and string char p[] = "hello"; string str(p); const char *c = str.c_str(); // 4 CString and int int i = atoi(s); // atof for floating point conversion int i = _ttoi(s); CString.Format("%d", i); // 5 CString and char[100] char a[100]; CString s("abc"); strncpy(a, (LPCTSTR)s, sizeof(a)); // vs2013 reports an error. Strncpy is required_ s
CStringList
CStringList is a linked list defined in MFC for storing CString strings
// use_CStringList.c // structure CStringList str_list; // Add or remove elements str_list.AddHead("123"); //Add an element at the head of the list str_list.AddTail("123"); //Add an element at the end of the list str_list.InsertBefor(POSITION pos, "123"); // Insert new element before positioning str_list.InsertAfter(POSITION pos, "123"); // Insert new element after positioning str_list.RemoveHead(); // Delete the head, tail and all elements respectively str_list.RemoveTail(); str_list.RemoveAll(); // visit str_list.GetHead(); // Get header and tail elements str_list.GetTail(); str_list.GetAt(POSITION pos); // Gets the element at the specified location str_list.SetAt(POSITION pos); // Sets the element at the specified location str_list.RemoveAt(POSITION pos); // Deletes the element at the specified location // Traversal used str_list.GetHeadPosition(); // Get the position of the head and tail elements str_list.GetTailPosition(); str_list.GetNext(POSITION pos); // Get next element str_list.GetPrev(POSITION pos); // Get previous element // lookup POSITION pos = str_list.Find("123"); // Gets the position of the element specified by the string POSITION pos = str_list.FindIndex(int i); // Gets the location of the element specified by the index // state str_list.GetCount(); // Returns the number of elements str_list.IsEmpty(); // Is the test list empty // ergodic POSITION pos; pos = str_list.GetHeadPosition(); while (pos != NULL) { CString s = str_list.GetNext(pos); printf("%s", s); }
appendix
1 How to solve the error MSB8031 when migrating VC6 to VS2013 2 CString class library for non MFC projects under VS2008 3 Usage of CString member function 4 CString to LPCSTR method supplement 5 CString Format function VS2013 6 CString to char *,strings 7 Detailed explanation of string function in C language 8 CSTRINGLIST usage 9 Conversion between CString,string,char * 10 MFC CString and int convert to each other
Thank you for your online friends!
A small problem
When writing this summary, there are ten links in the final appendix. I tested them on the local localhost. These ten links can only display six, It's the same as the last part of the page [, and it doesn't look the same as the last part of the page [) A space is added between them. Occasionally, ten links will appear normally, and then there will be no refresh. Finally, the display from deploy to github is normal The reason has not been found yet. Write down the problem first and then deal with it