string类的一些总结

一、初始化:

string s1("Hello");  
string month = "March"; 
string s2(8,’x’);//生成8个x的一个字符串

错误示范:
不可以用单个字符(即单引号扩起的字符初始化)

string error1 = ‘c’;  // 错

二、流操作:
1.用cin读到空格为止
2.单独的getline函数,可以读入空格。但是遇到诸如换行符等会停止(且换行符不存入!)

string s;
getline(cin ,s);   

三、赋值:
1.用字符串赋值
2.赋值单个字符
3.用assign成员函数特殊赋值(可以赋值特定的位数)

s2 = s1;
s = ‘n’;

string s1("cat"), s3;
s3.assign(s1);
string s1("catpig"), s3;
s3.assign(s1, 1, 3); 
//从s1 中下标为1的字符开始复制3个字符给s3

四、单个字符的修改与访问:
1.[]
2.at成员函数

s2[5] = s1[3] = ‘a’;
cout << s1.at(i) << endl;

五、字符串的连接:
1.s1+s2
2.append成员函数

s1 += s2;
s1.append(s2);
s2.append(s1, 3, s1.size());//s1.size(),s1字符数

六、字符串的比较:
1.直接用比较运算符来作比较,返回bool类型
2.使用compare成员函数,按照到小关系返回-1,0,1

bool b = (s1 == s2);

int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3); //s1 1-2; s3 0-3
//s1的从1开始两个字符与 s2从0开始3个字符比较
int f5 = s1.compare(0,s1.size(),s3);//s1 0-end
//s1所有字符与s3所有字符比较

输出
0 // hello == hello
1 // hello > hell
-1 // hell < hello
-1 // el < hell
1 // hello > hell

七、一些常用的成员函数:
1.substr 取子串:

string s1("hello world"), s2;
s2 = s1.substr(4,5); // 下标4开始5个字符
cout << s2 << endl;

输出:
o wor

2.swap 交换两个字符串

string s1("hello world"), s2("really");
s1.swap(s2);  

3.寻找字符/字符串的位置
①find 返回目标第一次出现的下标:

string s1("hello world");
s1.find("lo");

//从指定下标开始找目标字符/字符串
    cout << s1.find("ll",1) << endl;
	cout << s1.find("ll",2) << endl;  //从下标2开始找
    cout << s1.find("ll",3) << endl; 

在s1中从前向后查找 “lo” 第一次出现的地方,如果找到,返回 “lo”开始的位置,即 l 所在的位置下标。
如果找不到,返回 string::npos (string中定义的静态常量)
②rfind 从后向前找第一次出现的位置,其余同上

③寻找第一次出现在指定字符串里的字符的位置(较少使用):

s1.find_first_of(“abcd");
s1.find_last_of(“abcd");
s1.find_first_not_of(“abcd");
s1.find_last_not_of(“abcd");

5.erase 删除从某个位置开始的字符

     string s1("hello worlld");
     s1.erase(5);
     cout << s1;
     cout << s1.length();
     cout << s1.size();

// 去掉下标 5 及之后的字符
输出:
hello55

6.replace 替换字符:

string s1("hello world");
   s1.replace(2,3, “haha");
   cout << s1;

//将s1中下标2 开始的3个字符换成“haha” 不一定要等量代替
输出:
hehaha world

   string s1("hello world");
   s1.replace(2,3, "haha", 1,2);
   cout << s1;

// 将s1中下标2 开始的3个字符换成“haha” 中下标1开始的2个字符
输出:
heah world

7.insert 插入字符:

    string s1("hello world"); 
    string s2(“show insert");
    s1.insert(5,s2); 
    // 将s2插入s1下标5的位置
    cout << s1 << endl;
    s1.insert(2,s2,5,3); 
    //将s2中下标5开始的3个字符插入s1下标2的位置
    cout << s1 << endl;

8.转换为c风格的char*字符串:
①成员函数 c_str()

   string s1("hello world");
   printf("%s\n", s1.c_str());

// s1.c_str() 返回传统的const char * 类型字符串,且该字符串以‘\0’结尾。
②成员函数data 不知道有啥用

9.copy string拷贝到char*字符串:

    string s1("hello world");
    int len = s1.length();
    char * p2 = new char[len+1]; 
    s1.copy(p2,5,0); 
    p2[5]=0;
    cout << p2 << endl;

s1.copy(p2,5,0) 从s1的下标0的字符开始制作一个最长5个字符长度的字符串副本并将其赋值给p2。返回值表明实际复制字符串的长度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值