字符串和字符比较浅析测试(源码摘自microsoft help)
2012-08-20 11:29
330 查看
// basic_string_compare.cpp // compile with: /EHsc #include <string> #include <iostream> int main( ) { using namespace std; // The first member function compares // an operand string to a parameter string int comp1; string s1o ( "CAB" ); string s1p ( "CAB" ); cout << "The operand string is: " << s1o << endl; cout << "The parameter string is: " << s1p << endl; comp1 = s1o.compare ( s1p ); if ( comp1 < 0 ) cout << "The operand string is less than " << "the parameter string." << endl; else if ( comp1 == 0 ) cout << "The operand string is equal to " << "the parameter string." << endl; else cout << "The operand string is greater than " << "the parameter string." << endl; cout << endl; // The second member function compares part of // an operand string to a parameter string int comp2a, comp2b; string s2o ( "AACAB" ); string s2p ( "CAB" ); cout << "The operand string is: " << s2o << endl; cout << "The parameter string is: " << s2p << endl; comp2a = s2o.compare ( 2 , 3 , s2p ); if ( comp2a < 0 ) cout << "The last three characters of " << "the operand string\n are less than " << "the parameter string." << endl; else if ( comp2a == 0 ) cout << "The last three characters of " << "the operand string\n are equal to " << "the parameter string." << endl; else cout << "The last three characters of " << "the operand string\n is greater than " << "the parameter string." << endl; comp2b = s2o.compare ( 0 , 3 , s2p ); if ( comp2b < 0 ) cout << "The first three characters of " << "the operand string\n are less than " << "the parameter string." << endl; else if ( comp2b == 0 ) cout << "The first three characters of " << "the operand string\n are equal to " << "the parameter string." << endl; else cout << "The first three characters of " << "the operand string\n is greater than " << "the parameter string." << endl; cout << endl; // The third member function compares part of // an operand string to part of a parameter string int comp3a; string s3o ( "AACAB" ); string s3p ( "DCABD" ); cout << "The operand string is: " << s3o << endl; cout << "The parameter string is: " << s3p << endl; comp3a = s3o.compare ( 2 , 3 , s3p , 1 , 3 ); if ( comp3a < 0 ) cout << "The three characters from position 2 of " << "the operand string are less than\n " << "the 3 characters parameter string " << "from position 1." << endl; else if ( comp3a == 0 ) cout << "The three characters from position 2 of " << "the operand string are equal to\n " << "the 3 characters parameter string " << "from position 1." << endl; else cout << "The three characters from position 2 of " << "the operand string is greater than\n " << "the 3 characters parameter string " << "from position 1." << endl; cout << endl; // The fourth member function compares // an operand string to a parameter C-string int comp4a; string s4o ( "ABC" ); const char* cs4p = "DEF"; cout << "The operand string is: " << s4o << endl; cout << "The parameter C-string is: " << cs4p << endl; comp4a = s4o.compare ( cs4p ); if ( comp4a < 0 ) cout << "The operand string is less than " << "the parameter C-string." << endl; else if ( comp4a == 0 ) cout << "The operand string is equal to " << "the parameter C-string." << endl; else cout << "The operand string is greater than " << "the parameter C-string." << endl; cout << endl; // The fifth member function compares part of // an operand string to a parameter C-string int comp5a; string s5o ( "AACAB" ); const char* cs5p = "CAB"; cout << "The operand string is: " << s5o << endl; cout << "The parameter string is: " << cs5p << endl; comp5a = s5o.compare ( 2 , 3 , s2p ); if ( comp5a < 0 ) cout << "The last three characters of " << "the operand string\n are less than " << "the parameter C-string." << endl; else if ( comp5a == 0 ) cout << "The last three characters of " << "the operand string\n are equal to " << "the parameter C-string." << endl; else cout << "The last three characters of " << "the operand string\n is greater than " << "the parameter C-string." << endl; cout << endl; // The sixth member function compares part of // an operand string to part of an equal length of // a parameter C-string int comp6a; string s6o ( "AACAB" ); const char* cs6p = "ACAB"; cout << "The operand string is: " << s6o << endl; cout << "The parameter C-string is: " << cs6p << endl; comp6a = s6o.compare ( 1 , 3 , cs6p , 3 ); if ( comp6a < 0 ) cout << "The 3 characters from position 1 of " << "the operand string are less than\n " << "the first 3 characters of the parameter C-string." << endl; else if ( comp6a == 0 ) cout << "The 3 characters from position 2 of " << "the operand string are equal to\n " << "the first 3 characters of the parameter C-string." << endl; else cout << "The 3 characters from position 2 of " << "the operand string is greater than\n " << "the first 3 characters of the parameter C-string." << endl; cout << endl; }
运行结果:
The operand string is: CAB The parameter string is: CAB The operand string is equal to the parameter string. The operand string is: AACAB The parameter string is: CAB The last three characters of the operand string are equal to the parameter string. The first three characters of the operand string are less than the parameter string. The operand string is: AACAB The parameter string is: DCABD The three characters from position 2 of the operand string are equal to the 3 characters parameter string from position 1. The operand string is: ABC The parameter C-string is: DEF The operand string is less than the parameter C-string. The operand string is: AACAB The parameter string is: CAB The last three characters of the operand string are equal to the parameter C-string. The operand string is: AACAB The parameter C-string is: ACAB The 3 characters from position 2 of the operand string are equal to the first 3 characters of the parameter C-string.
相关文章推荐
- 字符串和字符的比较方法浅析测试(源码摘自microsoft help)
- 比较两个字符串A和B,确定A中是否包含B中所有的字符。
- 原创:sql中比较一个字符串中是否含有另一个字符串中的一个字符
- -比较第一与第二个字符串,是否有连续的5个字符相同,如果有
- 去除字符串中的重复字符——方法汇总和性能测试
- 比较第一与第二个字符串,是否有连续的5个字符相同.sql
- 第十七周项目2-5-比较两字符串,返回值同strcmp()(字符数组做形参)
- 汇编语言: 从键盘上输入一串字符(用回车键结束,使用 10 号功能调用。)放在 STRING 中,试 编制一个程序测试字符串中是否存在数字。如有,则把 CL 的第 5 位置 1,否则将该位置置 0。
- sas9正则表达式测试(对字符串进行字符替换)
- 字符串反转C++实现源码(带测试用例)
- 字符串与字符数组的区别(摘自c++primer)
- 【读书笔记】Linux Shell脚本攻略(二)读取字符,各种循环,分隔符和迭代器,比较和测试
- LDD3源码分析之字符设备驱动程序(加上测试代码)
- 快速比较两个字符串中字符完全相同(兄弟字符串比较)
- 657. Judge Route Circle(字符串转换成字符比较问题)
- 主函数main()中由数字和字母组成的字符串s为测试数据,程序 主要提取字符串s中由字符'$'分隔的不同八进制数形式的字符串, 将八进制数形式字符子串所对应的字符生成新的字符串
- 1/22 测试一(STL 模拟 贪心)B.(模拟 比较两个字符串,输出比较结果) Online Judge
- String源码比较字符串
- 24. PowerShell -- 使用特殊文本命令(-contains字符串比较,-like 匹配字符)