CString str = "Hello, world!"; CString delim = ","; CString token; int pos = 0; while ((pos = str.Find(delim)) != -1) { token = str.Left(pos); str = str.Right(str.GetLength() - pos - delim.GetLength()); // process token } // process last token
CString str = "The quick brown fox jumps over the lazy dog"; CString delim = " "; CString token; int start = 0; while ((token = str.Tokenize(delim, start)) != "") { // process token }This code uses the Tokenize function directly to split the CString object into substrings. The second parameter of the function indicates the starting position for the search. The code can be used, for example, to iterate over the words in a sentence. The CString Tokenize function is part of the MFC library, which is included with Microsoft Visual Studio.