跳到正文

个人记录

string

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	string s1="Hello";
	string s2="world!";
	string s3=s1+s2;
	cout<<s3<<endl;
	s3.append("123");
	cout<<s3<<endl;


//	cin>>s3;
//	cout<<s3<<endl;


//	getline(cin,s3);
//	cout<<s3<<endl;

	int lens1=s1.length();
	cout<<lens1<<endl;
	int lens2=s2.size();
	cout<<lens2<<endl;


	cout<<s1[0]<<endl;
	cout<<s1[2]<<endl;

	char a[100];
	int lens=s1.copy(a,s1.size());
	a[lens]='\0';
	printf("%s\n",a);

	strcpy(a,s2.c_str());
	printf("%s\n",a);

	s1="abc";
	s2="abb";
	if(s1>s2){
		cout<<"yes1"<<endl;
	}
	s1="abc";
	if(s1>s2){
		cout<<"yes2"<<endl;
	}

	s1="Hello world";
//	s2=s1.sudstr(2,5);
//	cout<<s2<<endl;
//	s2=s1.sudstr(3);
//	cout<<s2<<endl;

	int n=s1.find("world");
	cout<<n<<endl;
	cout<<s1.find("o")<<endl;
	cout<<s1.rfind("o")<<endl;
	cout<<s1.find("oooo")<<endl;

	s1="hello World";
	s1.replace(0,5,"school");
	cout<<s1<<endl;

	s1="hello world";
	s1.erase(3,3);
	cout<<s1<<endl;

	s1="hello world";
	s1.insert(5,"aaaa");
	return 0;
}
原文