C++ 字符串与数组操作全解析
1. 字符串操作基础
在 C++ 中,字符串操作是非常常见且重要的任务。下面我们将详细介绍一些常用的字符串操作函数及其使用方法。
1.1 字符串修改操作
以下代码展示了一系列字符串修改操作:
s1.erase(0, 7); //remove “Quick! “
s1.replace(9, 5, s2); //replace “Count” with “Lord”
s1.replace(0, 1, “s”); //replace ‘S’ with ‘s’
s1.insert(0, s3); //insert “Don’t “ at beginning
s1.erase(s1.size()-1, 1); //remove ‘.’
s1.append(3, ‘!’); //append “!!!”
int x = s1.find(‘ ‘); //find a space
while( x < s1.size() ) //loop while spaces remain
{
s1.replace(x, 1, “/”); //replace with slash
x = s1.find(‘ ‘); //find next space
}
cout << “s1: “ << s1 << endl;
return 0;
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



