c++字符串学习笔记

字符串(string 类)

声明

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

string str1;//生成空字符串
cin << str1;
cout << str1;

string str2("hello");//生成并初始化
cout << str2;

string str3(str2);//hello
cout << str3;

string str4(10,'H');//HHHHHHHHHH
cout << str4;

char a[] = "hello";//hello
string str5(a);
cout << str5;

连接(追加):append()函数

string str("Hello");
char sz[] = "Word";
string str6("Word");

str.append(10,'W');//追加单个字符

str.append(sz);//追加c风格字符串

str.append(str6);//追加字符串

比较:compare()函数

返回值:0、1、-1

前 > 后 返回1、前 < 后 返回-1、前 = 后 返回0;

string date1("21");
string date2("22");
string date3("22");
char date4[] = "23";

cout << date1.compare(date2) << endl;//-1
cout << date2.compare(date1) << endl;//1
cout << date2.compare(date3) << endl;//0
cout << date3.compare(date4) << endl;//-1

查找:find()函数

string findStr("ABCDEF");
cout << findStr.find('D', 2) << findStr.find('D', 5);
//第一个参数是要查找的字符,第二个参数是起始查找的位置。返回值为该字符在字符串中的位置int(0位起),若无则返回-1.

char findChar = "BCD";
cout << findStr.find(findChar, 0);
//返回字串在string中的位置,本题为1

string findStr1("ABC");
cout << findStr.find(findStr1, 0);

替换:replace()函数

string replaceStr("ABCDEFG");
cout << replaceStr.replace(2, 5, "2345");//先删除C-E,在插入2345. AB2345FG。
cout << replaceStr.replace(2, 5, 10, 'M');//先删除,再插入10个M

插入:incert()函数

string incertStr("123456");
string incertStr1("ABCDEF");
cout << incertStr.incert(3, incertStr1);

删除 & 提取子串:erase() & substr()函数

string str_test("ABCDEFG");
str_test.erase(3, 3); //从D起 删除3个字符

str_test.substr(3, 3);//返回string, 提取DEF。

获取字符串长度 str.length()

image-20210815211032501

image-20210817154349696

字符串流(常用于类型转换)

注意

  1. 再进行多次转换的时候,必须调用stringstream的成员函数clear().
  2. clear()重置流的标志状态;str()清空流的内存缓冲,重复使用内存消耗不再增加!
  3. 在多次数据类型转换的场景下,必须使用 clear() 方法清空 stringstream,不使用 clear()方法或使用 str("") 方法,都不能得到数据类型转换的正确结果。
#include<sstream> 
stringstream stream;
stream.clear();
stream.str("");
用法1 数据类型的转换
// int -> string
stringstream sstream;
string strResult;
int nValue = 1000;
 
// 将int类型的值放入输入流中
sstream << nValue;
// 从sstream中抽取前面插入的int类型的值,赋给string类型
sstream >> strResult;
用法2 多字符串的拼接
stringstream sstream;
// 将多个字符串放入 sstream 中
sstream << "first" << " " << "string,";
sstream << " second string";
// 可以使用 str() 方法,将 stringstream 类型转换为 string 类型;
cout << "strResult is: " << sstream.str() << endl;
用法3 可以用于分割被空格、制表符等符号分割的字符串
#include<iostream>  
#include<sstream>        //istringstream 必须包含这个头文件
#include<string>  
using namespace std;  
int main(){  
    string str="i am a boy";  
    istringstream is(str);  
    string s;  
    while(is>>s)  {  
        cout<<s<<endl;  
    }  
} 
用法4 实现任意类型的转换
 template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }

int main(){
    string s = "1 23 # 4";
    stringstream ss;
    ss<<s;
    while(ss>>s){
        cout<<s<<endl;
        int val = convert<int>(s);
        cout<<val<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wzx_1210

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值