- 注册时间
 - 2021-4-16
 
- 最后登录
 - 2024-11-28
 
- 在线时间
 - 1 小时
 
 
 
 
 
编程入门 
  
	- 龙马币
 - 150 
 
 
 
 
 | 
 
 
mfc cstring字符串查找多个内容,找到就返回真。 
 
要从CString中查找多个内容并返回布尔值,你可以使用Find方法多次,每次查找一个不同的子字符串。如果所有子字符串都被找到,你可以返回true;否则,返回false。以下是一个示例函数,它接受一个CString和一个字符串数组,并检查CString是否包含数组中的所有字符串: 
 
- #include <afxwin.h>
 
 - #include <vector>
 
 - #include <algorithm>
 
  
- bool FindAllSubstringsInCString(const CString& str, const std::vector<CString>& substrings)
 
 - {
 
 -     // 遍历所有要查找的子字符串
 
 -     for (const auto& substr : substrings)
 
 -     {
 
 -         // 如果找不到任何一个子字符串,则返回false
 
 -         if (str.Find(substr) == -1)
 
 -         {
 
 -             return false;
 
 -         }
 
 -     }
 
 -     // 如果所有子字符串都被找到,则返回true
 
 -     return true;
 
 - }
 
  复制代码- int main()
 
 - {
 
 -     CString mainString(_T("This is a sample string with multiple words."));
 
 -     std::vector<CString> substringsToFind;
 
 -     substringsToFind.push_back(_T("sample"));
 
 -     substringsToFind.push_back(_T("multiple"));
 
 -     substringsToFind.push_back(_T("words"));
 
  
-     // 调用函数查找所有子字符串
 
 -     bool allFound = FindAllSubstringsInCString(mainString, substringsToFind);
 
  
-     // 输出结果
 
 -     if (allFound)
 
 -     {
 
 -         AfxMessageBox(_T("All substrings found!"));
 
 -     }
 
 -     else
 
 -     {
 
 -         AfxMessageBox(_T("One or more substrings not found."));
 
 -     }
 
  
-     return 0;
 
 - }
 
  复制代码 
在这个例子中,FindAllSubstringsInCString函数接受一个CString(str)和一个CString向量(substrings),该向量包含要查找的所有子字符串。函数遍历substrings向量,并使用Find方法检查str是否包含每个子字符串。如果str不包含任何一个子字符串,函数会立即返回false。如果所有子字符串都被找到,函数会返回true。 
 
请注意,这个实现假设substrings向量中的字符串是唯一的,即没有重复的字符串。如果有重复的字符串,并且你只想检查str是否至少包含一次每个唯一的子字符串,你可能需要在将子字符串添加到substrings向量之前先进行去重操作。 
 
 |   
 
 
 
 |