Homework Introduction
字符串
拼接
string s = "hello world";
cout << s.size() << endl;//11
s = s + "ABC";
cout << s << endl;//hello worldABC
find 查找
string s = "hello world";
//查找
cout << s.find("wor") << endl;//6 下标
cout << s.find("orz") << endl;//18446744073709551615
cout << s.npos << endl; //18446744073709551615
int k = s.find("orz") ;
cout << k << endl; // -1
if(s.find("orl") != s.npos ){
cout << "找到了\n" ;
}else{
cout << "找不到\n";
}
erase 删除
string s = "hello world";
//删除
s.erase(2,6) ;//从下标2开始删, 删除6个字符
cout << s << endl;//herld
replace 修改
string s = "hello world";
//修改
s.replace(2,6,"ABC") ;//从下标2开始的后面6个字符,修改成ABC
cout << s << endl;//heABCrld
insert 插入
string s = "hello world";
//插入
s.insert(3,"ABC") ;//在下标3前面,插入ABC
cout << s << endl;//helABClo world
Problem
- Status
- Done
- Problem
- 28
- Open Since
- 2025-3-14 0:00
- Deadline
- 2025-4-27 23:59
- Extension
- 24 hour(s)