#include<iostream> #include<stdlib.h> #include<assert.h> using namespace std; class String { public: String(const char* str) :_str(new char[strlen(str) + 1]) { _size = strlen(str); _capacity = _size + 1; strcpy(_str, str); } String(const String& s) :_str(NULL) { String tmp(s._str); swap(_str, tmp._str); } ~String() { if (_str) { delete[] _str; _size = 0; _capacity = 0; _str = NULL; } } //删除单个字符 /*void Erase(size_t pos) { assert(pos <=_size); //pos>_size报错 int begin = pos; while (begin <= _size) { _str[begin] = _str[begin + 1]; begin++; } --_size; }*/ //删除字符串 void Erase(size_t pos, int len) { assert(pos <= _size); int len1 = len + 1; while (len1--) { _str[pos] = _str[pos + len +1]; pos++; } _size = _size - len; } char* C_str() { return _str; } private: char* _str; int _size; int _capacity; }; void Test() { String s("abcdefghijk"); s.Erase(2,4); cout << s.C_str() << endl; } int main() { Test(); system("pause"); return 0; }
C++【String类】String删除单个字符,删除字符串的函数实现
原创
©著作权归作者所有:来自51CTO博客作者韩静静的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
【C语言加油站】详细解读字符函数与字符串函数
【C语言加油站】字符函数与字符串函数详细介绍字符函数与字符串函数的相关知识点……
C语言 字符串 字符函数 字符串函数 -
C++ string字符串
string是非常强大的类型,很好的封装了字符串的操作
string c语言 字符串 bc ci -
【C++宝藏男孩】C++ string字符串比较
转载:谁吃薄荷糖 文章
笔记 产品经理 API接口 字符串 python -
C++ | C++字符串
C++字符串C++ 提供了以下两种类型的字符串表示形式:C 风格字符串C++ 引入的 str
c++ 字符串 #include g++ -
string替换所有指定字符串(C++)
C++的
字符串 #include ios -
C++ string数组字符串排序 sort
功能:给定一个string的字符数组(左图),返回一个已经排序好的string的字符数组(右图
c++ 开发语言 后端 #include 初始化 -
Android String 去掉部分字符串 string移除字符串
接上一篇文章继续说String类 六,字符串的删除字符串的删除是通过Remove方法实现的,格式为: (1)字符串.Remove(开始位置)  
c# string stringbuilder visual studio .net framework