版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/88700890
一 第一个C++字符串例子
1 代码
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring = "This is a string";
cout << mystring<<endl;
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
This is a string
二 第二个C++字符串例子
1 代码
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring =
"This is the initial string content";
cout << mystring << endl;
mystring =
"This is a different string content";
cout << mystring << endl;
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
This is the initial string content
This is a different string content
三 一个简单字符串例子
1 代码
#include <iostream>
using namespace std;
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
Greeting message: Hello
四 使用字符串函数的例子
1 代码
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[11] = "Hello";
char str2[11] = "World";
char str3[11];
int len ;
// 复制str1到str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// 连接str1 和 str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// 连接或str1的总长度
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
五 find函数测试
1 代码
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"find test"<<endl;
//测试size_type find (charT c, size_type pos = 0) const noexcept;
string st1("babbabab");
cout << st1.find('a') << endl;//1 由原型知,若省略第2个参数,则默认从位置0(即第1个字符)起开始查找
cout << st1.find('a', 0) << endl;//1
cout << st1.find('a', 1) << endl;//1
cout << st1.find('a', 2) << endl;//4 在st1中,从位置2(b,包括位置2)开始,查找字符a,返回首次匹配的位置,若匹配失败,返回npos
cout << st1.rfind('a',7) << endl;//6 关于rfind,后面讲述
cout << st1.find('c', 0) << endl;//18446744073709551615
cout << (st1.find('c', 0) == -1) << endl;//1
cout << (st1.find('c', 0) ==18446744073709551615) << endl;//1 两句均输出1,原因是计算机中-1和18446744073709551615都表示为64个1(二进制)
cout << st1.find('a', 100) << endl;//18446744073709551615当查找的起始位置超出字符串长度时,按查找失败处理,返回npos
//测试size_type find (const basic_string& str, size_type pos = 0) const noexcept;
string st2("aabcbcabcbabcc");
string str1("abc");
cout << st2.find(str1, 2) << endl;//6 从st2的位置2(b)开始匹配,返回第一次成功匹配时匹配的串(abc)的首字符在st2中的位置,失败返回npos
//测试size_type find (const charT* s, size_type pos = 0) const;
cout << st2.find("abc", 2) << endl; //6 同上,只不过参数不是string而是char*
//测试size_type find (const charT* s, size_type pos, size_type n) const;
cout << st2.find("abcdefg", 2, 3) << endl;//6 取abcdefg得前3个字符(abc)参与匹配,相当于st2.find("abc", 2)
cout << st2.find("abcbc", 0, 5) << endl;//1 相当于st2.find("abcbc", 0)
cout << st2.find("abcbc", 0, 6) << endl;//18446744073709551615第3个参数超出第1个参数的长度时,返回npos
return 0;
}
2 运行
[root@localhost test]# g++ test.cpp -o test
[root@localhost test]# ./test
find test
1
1
1
4
6
18446744073709551615
1
0
18446744073709551615
6
6
6
1
18446744073709551615