判断字符串匹配判断 c++
2017-10-11 15:48
246 查看
环境:
centos
c++
2 字符串匹配到起始位置
3 字符串匹配到末尾位置
4 字符串匹配在中间位置
5 字符串未能匹配到
除了上面的code中实例外还可以判断条件
2 对于中间位置匹配
基于前面的 3 种条件过滤,4种判断条件也可以改为
centos
c++
匹配情况分类
1 字符串完全匹配 compare2 字符串匹配到起始位置
3 字符串匹配到末尾位置
4 字符串匹配在中间位置
5 字符串未能匹配到
code
#include <iostream> #include <string> int main() { string str = "abcdef中国汽车"; string t = "bc"; pos = str.find(t); if(!str.compare(t)) { std::cout << "1 str is equal to t" << std::endl; } else if(pos == 0) { std::cout << "2 t is match at the begin of str" << std::endl; } else if(pos + t.length() == str.length()) { std::cout << "3 t is match at the begin of str" << std::endl; } else if(pos != string::npos) { std::cout << "4 t is match at the middle of str" << std::endl; } else { std::cout << "5 t is not matched in str" << std::endl; } return 0; }
补充说明
1 对于完全匹配除了上面的code中实例外还可以判断条件
if(pos==0 && pos + t.length() == str.length() ) { std::cout << "1 str is equal to t" << std::endl; }
2 对于中间位置匹配
基于前面的 3 种条件过滤,4种判断条件也可以改为
if(pos > 0 && pos < str.length()) { std::cout << "4 t is match at the middle of str" << std::endl; }
相关文章推荐
- C++中用栈来判断括号字符串匹配问题的实现方法
- C++中用栈来判断括号字符串匹配问题
- 算法串匹配C++实现字符串匹配的KMP算法
- C++判断是否中文字符
- 字符串匹配Sunday算法C++实现
- C++判断输入utf8字符的字节数
- [Ant][StartWithAnt] 第五章 条件判断Contidion 3.字符串匹配
- c++字符判断
- 字符串匹配Sunday算法C++实现
- C++对cin输入字符的判断及分段函数处理方法示例
- 字符串操作(人民币转成大写/全角字符转半角字符/去掉字符串中重复的子字符串/过滤常见特殊字符/反过滤特殊字符/判断是不是合法手机/字符串匹配的算法)
- 使用正则表达式判断字符串匹配
- 【编程题】-C++实现:判断字符串在末尾加一个字符,能否构成回文串
- 算法导论-第32章-字符串匹配:有限自动机方法(改进版本:预处理阶段复杂度为O(m*| ∑ |) )C++实现
- C++_字符串匹配_忽略大小写_方法
- C/C++ —语言判断数字或字符的函数总结
- c++ primer plus 第五版 第七章 编程练习 第 8 题 (字符数组转成字符串,trim使用,判断空行)
- c++如何判断一个字符串是否只有可见字符
- CCF201409-3 字符串匹配 初级KMP 一句判断大小写锁定
- C/C++字符串匹配和替换