跳到正文

个人记录

srting的应用

ban_user约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年11月23日
收录
2025年12月12日
更新
2026年10月21日
篇幅
约 2 分钟

版本

  1. v2当前
  2. v1
C++
#include<iostream>
using namespace std;
int main(){
	//字符串的拼接
	string s1 = "Hello";
	string s2 = "world";
	string s3 = s1 + s2;
	cout << s3 << endl;
	s3.append("12345");
	cout << s3 << endl;
	//字符串输出
	cin >> s3;//cin输入字符出,一空二和回车结束
	cout << s3 << endl;
	//读入一这行字符
	getline(cin,s3);
	cout << s3 << endl;
	//字符串的长度函数
	cout << s1.length() << endl;
	cout << s2.size() << endl;
	//s.当作字符数组
	cout << s1[0] << endl;
	cout << s2[2] <<endl;
	//字符串和字符数组可以互换
	char a[100];
	int lens = s1.copy(a,s1.size());
	printf("%s\n",a);

	strcpy(a,s2.c_str());
	printf("%s",a);
	//字符串的比较
	s1 = "abc";
	s2 = "abb";
	if(s1 > s2)cout << "yes1"<<endl;
	s1 ="Abc";
	if(s1 > s2)cout << "yes2"<<endl;
	//求子串
	s1 = "hellow world";
	s2 = s1.substr(2,5);
	cout << s2 << endl;
	s2 = s1.substr(3);
	cout << s2 << endl;
	//9.查找字符串字串
	cout << s1.find("world") << endl;
	cout << s1.find("o")<<endl;
	cout << s1.rfind("o") << endl;
	//替换
	s1.replace(0,5,"school");
	cout << s1 << endl;

	//删除
	s1.erase(3,2);
	cout << s1 << endl;
	//插入
	s1.insert(5,"aaaaaaaaaaaaa");
	cout << s1 << endl;
	return 0;
}
代码
原文