在CString中查找是否有特定字符串

//原始字符串
CString strType  = "string0string1string2";

//假如string0不存在、string1不存在、string2存在
if (strType.Find(_T("string0")) < 0
	&& strType.Find(_T("string1")) < 0 
	&& strType.Find(_T("string2")) >= 0)
{
//假如string3不存在
	if(strType.Find(_T("string3")) < 0)
		continue;
}

//假如string4存在
if(strType.Find(_T("string4")) >= 0)
	continue;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
判断字符串是否符合特定格式可以使用正则表达式、字符串匹配、字符遍历等方法。以下是几种常见的方式: 1. 正则表达式:使用正则表达式匹配字符串是否符合特定模式。C++ 可以使用 std::regex 类来进行正则表达式匹配。 ```cpp #include <regex> #include <iostream> int main() { std::string str = "1234-5678-9012-3456"; std::regex pattern("\\d{4}-\\d{4}-\\d{4}-\\d{4}"); if (std::regex_match(str, pattern)) { std::cout << "Match!" << std::endl; } else { std::cout << "Not match!" << std::endl; } } ``` 2. 字符串匹配:使用字符串库函数如 strstr()、strchr() 等查找字符串是否包含特定的子串。 ```cpp #include <iostream> #include <cstring> int main() { std::string str = "hello world"; char substr[] = "world"; if (std::strstr(str.c_str(), substr) != nullptr) { std::cout << "Match!" << std::endl; } else { std::cout << "Not match!" << std::endl; } } ``` 3. 字符遍历:遍历字符串的每一个字符,检查是否符合特定要求。 ```cpp #include <iostream> #include <cstring> int main() { std::string str = "1234-5678-9012-3456"; bool is_valid = true; for (int i = 0; i < str.size(); i++) { if (i % 5 != 4 && !std::isdigit(str[i])) { is_valid = false; break; } if (i % 5 == 4 && str[i] != '-') { is_valid = false; break; } } if (is_valid) { std::cout << "Valid format!" << std::endl; } else { std::cout << "Invalid format!" << std::endl; } } ``` 以上三种方法都可以用来判断字符串是否符合特定格式,具体选择哪一种方法取决于具体的需求和实现复杂度。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值