您的位置:首页 > 其它

字符串

2016-03-18 18:20 351 查看
[b] 一.字符串常用函数[/b]

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;

int main()
{
string s(4,'x');
cout << s << endl;//xxxx

s += "xx";

string buffer = "E:\\1.txt";
ifstream infile;
infile.open(buffer.c_str());//返回

//erase
string s1 = "This is a test string";
s1.erase(10, 5);
cout << s1 << endl;//This is a string

//Insert
string s2 = "test ";
s1.insert(10, s2);
cout << s1 << endl;//This is a test string

//replace
s1 = "This is a string";
s1.replace(10, 6, s2);
cout << s1 << endl;//This is a test

//swap
s1.swap(s2);
cout << s1 << endl; // test

s2[2] = 'a'; s2[3] = 't';
cout << s2 << endl;//That is a test

//substr
s1 = s2.substr(10, 4);
cout << s1 << endl;//test

//find(子串,初始查找位置)
cout << s2.find("is", 0) << endl;//5
cout << s2.find("at", 0) << endl;//2

//rfind(子串,初始查找位置)从后往前找
cout << s2.rfind("a", 13) << endl;//8
cout << s2.rfind("t", 13) << endl;//13

//find_first_of,find_first_not_of
//find_last_of,find_last_not_of
s1 = "abcdef";
cout << s1.find_first_of("xeyz") << endl;//4
cout << s1.find_first_not_of("xyba") << endl;//2
cout << s1.find_last_of("xeyz") << endl;//4
cout << s1.find_last_not_of("xyba") << endl;//5

//string.h strlen, strcmp, strcpy, atoi, strtok, strcat
cout << strlen(s1.c_str()) << endl;//6 abcdef

//c_str()是const char*
char s3[500];
strcpy(s3, s1.c_str());
cout << s3 << endl;//abcdef

char s4[500];
strcpy(s4, s2.c_str());
cout << strcmp(s3, s3) << endl;//0
cout << strcmp(s3, s4) << endl;//1

cout << strcat(s3, s4) << endl;//abcdefThat is a test

strcpy(s3, "123");
cout << atoi(s3) << endl;//123

s1 = "123 456,789,你好";
strcpy(s3, s1.c_str());
char *p = strtok(s3, " ");
while((p = strtok(NULL, ",")))
{
cout << p << endl;
/*
123
456
789
你好
*/
}
return 0;
}


[b] 二.getline[/b]

用来读入一整行到string类型变量中去,若getline没有读入字符,它将返回false,可用于判断是否结束以终止程序。

cin在getline之前使用,getline无法工作,cin的回车getline会读入,需要cin.ignore()

#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{

string str;
cin >> str1; //Ed wood
cout << str1; //Ed, 自动忽略空格和之后内容

return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main(int argc, char *argv[])
{

string str;
getline(cin, str); //Ed wood
cout << str; //Ed wood

return 0;
}


#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{

ifstream infile;
ofstream outfile;

string buffer;

infile.open("E:\\3.txt");
outfile.open("E:\\2.txt", ios::app);

while(getline(infile, buffer))
{
outfile << buffer << endl;
/*
Num = 1
Num = 2
Num = 3
Num = 4
Num = 5
1
2
3
4
5

*/
}

infile.close();
outfile.close();

return 0;
}


[b]三.字符串++[/b]

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
string s1 = "1", s2 = "2";
char s3[] = "3";//char[2]

//左边必须string,+=同上
s1 = s3;//ok
s1 = s3;//ok
s1 = 'l';//ok
s3 = s1;//error

string s4 = "";

s1 = s2 + s3;//ok
s1 = s2 + s4;//ok
s1 = 'w' + s3;//ok
s1 = "123" + "456";//error

return 0;
}


[b]四.补充[/b]

#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
using namespace std;

int main()
{
//assign
string s1 = "cat";
string s2;
s2.assign(s1);
cout << s2 << endl;//cat
s2.assign(s1, 0, 2);
cout << s2 << endl;//ca

//append
s1.append(s2);
cout << s1 << endl;//catca
s2.append(s1, 3, s1.size());//3至结束
cout << s2 << endl;//caca

//length,size
cout << s1.length() << endl;//5
cout << s1.size() << endl;//5

//strstr
//cout << strstr(s1.c_str(), s2.c_str()) << endl;//返回NULL
s1.append(s2);
cout << strstr(s1.c_str(), s2.c_str()) << endl;//返回s2在s1中首次出现的地址,cacaca

//at
string s3("hello");
cout << s3[0] << " " << s3.at(0) << endl;//h h
//cout << s3.at(7) << endl;//at会做范围检查,超出范围会抛出outofrange异常,下标运算符不会

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: