问题标题: 酷町堂:字符串函数(string和char数组)(所有)

1
1

0
已采纳
周琪岳
周琪岳
资深光能
资深光能
  1. 字符串拼接:
    字符串可以直接使用’’+’’(连接号)对两个字符串变量进行连接,返回连接后的新字符串变量。
    如: string a=“hello”,b=“world”;
    string c=a+b;
    cout<<c;
    //输出c的值是helloworld,是把字符串a和b连接起来

  2. 字符串比较大小:
    两个字符串可以通过关系运算符(6种)来进行字典序大小的比较(比较字典序,即比较两个字符的ascii码值的大小),如果关系成立,则返回true,如果关系不成立,则返回false。
    比较两个字符串的大小的规则:
    首先比较两个字符串第一个字符的字典序大小,如果相同,再比较第二个字符的字典序大小,如果还相同,再比较第三个字符的字典序大小……如果直到较短的字符串所有字符比较完都是相等的,那么较长的字符串大。在一个个字符比较的过程中,只要出现不相等的情况,那么该次比较中字符ascii码值大的字符串大。
    比如:“123”<“23” “1234”>“123”

  3. empty 函数:

  • 判断字符串是否为空字符串的函数:empty()
  • 格式:字符串名.empty(); //如:s.empty();
  • 功能:若s为空串(只有结束符的字符串)则返回true/1,否则返回false/0。
    如:
string a="",b="123";
cout<<a.empty()<<endl;  //输出为1
cout<<b.empty()<<endl;  //输出为0
//别忘了加小括号
  1. substr 函数:
  • 截取子串函数:substr(开始截取的位置,截取的长度)
  • 格式:字符串.substr(index,len);
  • 功能:从字符串1中index下标所对应的元素开始,截取之后len个长度的字符得到一个新的字符串常量,直接输出或者定义另外一个字符串变量来进行存储,且原字符串不发生变化。
    如:
string a,b;
a="hello";
b=a.substr(0,2);    //截取a中从下标0开始的2个字符,赋值给b
cout<<a<<endl;   //输出为hello,原字符串不变
cout<<b;          //输出为he
  1. erase 函数:
  • 删除部分字符串的函数:erase(开始删除的位置,删除的长度)
  • 格式:字符串1.erase(index,len);
  • 功能:删除字符串1中以index下标对应的元素为开始的len个长度的字符。

如:

string a;
a="hello";
a.erase(0,2);    //删除a中从下标0开始的2个字符
cout<<a<<endl;   //输出为llo,原字符串改变
  1. insert 函数:
  • 插入函数:insert(插入的位置对应元素的下标,插入的字符串)
  • 格式:字符串1.insert(index,字符串2);
  • 功能:在字符串1中的index下标对应的元素之前插入字符串2。如:
 

string a,b; a="hello"; b="123"; a.insert(1,b); //在字符串a下标为1的位置插入字符串b cout<<a; //输出a的结果为h123ello

  1. replace 函数:
  • 替换函数:replace(从什么位置开始替换,原字符串被替换的字符的长度,另一个用于替换的字符串)
  • 格式:字符串1.replace(pos,len,字符串2);//注意参数顺序
  • 功能:将字符串1中pos下标开始的len个字符替换为字符串2,如:
 

string a,b; a="hello"; b="123"; a.replace(2,1,b); //把从下标为2的字符开始的1个字符替换成字符串b cout<<a; //输出为he123lo

 字符串查找函数:在字符串中查找另一个字符串的位置(下标)
 找到,返回第一次出现的首字符对应的下标
 找不到,返回-1;
使用举例:
 string s=“hello world!”,s1=“lo”;
 int a=s.find(s1);//在s中查找s1第一次出现的位置 将返回的下标装到整型变量a中
 cout<<a;
find使用注意事项:
 需要将得到的结果放到一个int类型的整型变量中,否则如果找不到会输出一个很大的数表示负数
遍历字符串方法:

1
侯平仄
侯平仄
新手天翼
新手天翼
  1. string str;  
  2. 1. 字符串长度  
  3.    len = str.length();  
  4.    len = str.size();  
  5.   
  6. 2. 字符串比较  
  7.    可以直接比较  
  8.    也可以:  
  9.    str1.compare(str2);   
  10.    str1.compare(pos1,len1,str2,pos2,len2); 值为负,0 ,正。  
  11.    nops 长度到完。  
  12.   
  13. 3. 附加  
  14.    str1 += str2;  
  15.    或  
  16.    str1.append(str2);  
  17.    str1.append(str2.pos2,len2);  
  18.      
  19. 4. 字符串提取  
  20.    str2 = str1.substr();  
  21.    str2 = str1.substr(pos1);  
  22.    str2 = str1.substr(pos1,len1);  
  23.    string a=s.substr(0,4);       //获得字符串s中 从第0位开始的长度为4的字  
  24.   
  25. 符串  
  26.   
  27.   
  28. 5. 字符串搜索  
  29.    where = str1.find(str2);  
  30.    where = str1.find(str2,pos1); pos1是从str1的第几位开始。  
  31.    where = str1.rfind(str2); 从后往前搜。  
  32.   
  33. 6. 插入字符串  
  34.    不是赋值语句。  
  35.    str1.insert(pos1,str2);  
  36.    str1.insert(pos1,str2,pos2,len2);  
  37.    str1.insert(pos1,numchar,char);    numchar是插入次数,char是要插入的字  
  38.   
  39. 符。  
  40.   
  41. 7. 替换字符串  
  42.    str1.replace(pos1,str2);  
  43.    str1.replace(pos1,str2,pos2,len2);  
  44.   
  45. 8. 删除字符串  
  46.    str.erase(pos,len)  
  47.    str.clear();  
  48.   
  49. 9. 交换字符串  
  50.    swap(str1,str2);  
  51.   
  52. 10. C --> C++  
  53.    char *cstr = "Hello";  
  54.    string str1;  
  55.    cstr = cstr;  
  56.    string str2(cstr);  
  57.   
  58. 对于ACMer来说,C的字符串处理要比C++的方便、简单,尽量用C的字符串处理函数。  
  59.   
  60. C++中string类常用算法  
  61. string类的构造函数:  
  62.   
  63. string(const char *s);    //用c字符串s初始化  
  64.   
  65. string(int n,char c);     //用n个字符c初始化  
  66.   
  67. 此外,string类还支持默认构造函数和复制构造函数,如string s1;string   
  68.   
  69. s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出  
  70.   
  71. length_error异常  
  72.   
  73. string类的字符操作:  
  74.   
  75. const char &operator[](int n)const;  
  76.   
  77. const char &at(int n)const;  
  78.   
  79. char &operator[](int n);  
  80.   
  81. char &at(int n);  
  82.   
  83. operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,  
  84.   
  85. 当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。  
  86.   
  87. const char *data()const;//返回一个非null终止的c字符数组  
  88.   
  89. const char *c_str()const;//返回一个以null终止的c字符串  
  90.   
  91. int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符  
  92.   
  93. 拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目  
  94.   
  95. string的特性描述:  
  96.   
  97. int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元  
  98.   
  99. 素个数)  
  100.   
  101. int max_size()const;    //返回string对象中可存放的最大字符串的长度  
  102.   
  103. int size()const;        //返回当前字符串的大小  
  104.   
  105. int length()const;       //返回当前字符串的长度  
  106.   
  107. bool empty()const;        //当前字符串是否为空  
  108.   
  109. void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的  
  110.   
  111. 部分string类的输入输出操作:  
  112.   
  113. string类重载运算符operator>>  //用于输入,同样重载运算符operator<<用于输出  
  114.   
  115. 操作。  
  116.   
  117. 函数getline(istream &in,string &s);//用于从输入流in中读取字符串到s中,以换  
  118.   
  119. 行符'\n'分开。  
  120.   
  121.    
  122.   
  123. string的赋值:  
  124.   
  125. string &operator=(const string &s);//把字符串s赋给当前字符串  
  126.   
  127. string &assign(const char *s);//用c类型字符串s赋值  
  128.   
  129. string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值  
  130.   
  131. string &assign(const string &s);//把字符串s赋给当前字符串  
  132.   
  133. string &assign(int n,char c);//用n个字符c赋值给当前字符串  
  134.   
  135. string &assign(const string &s,int start,int n);//把字符串s中从start开始的  
  136.   
  137. n个字符赋给当前字符串  
  138.   
  139. string &assign(const_iterator first,const_itertor last);//把first和last迭  
  140.   
  141. 代器之间的部分赋给字符串  
  142.   
  143.    
  144.   
  145. string的连接:  
  146.   
  147. string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾  
  148.   
  149. string &append(const char *s);   //把c类型字符串s连接到当前字符串结尾  
  150.   
  151. string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前  
  152.   
  153. 字符串结尾  
  154.   
  155. string &append(const string &s);    //同operator+=()  
  156.   
  157. string &append(const string &s,int pos,int n); //把字符串s中从pos开始的n个  
  158.   
  159. 字符连接到当前字符串的结尾  
  160.   
  161. string &append(int n,char c);        //在当前字符串结尾添加n个字符c  
  162.   
  163. string &append(const_iterator first,const_iterator last);//把迭代器first和  
  164.   
  165. last之间的部分连接到当前字符串的结尾  
  166.   
  167.    
  168.   
  169. string的比较:  
  170.   
  171. bool perator==(const string &s1,const string &s2)const;//比较两个字符串是  
  172.   
  173. 否相等  
  174.   
  175. 运算符">","<",">=","<=","!="均被重载用于字符串的比较;  
  176.   
  177. int compare(const string &s) const;//比较当前字符串和s的大小  
  178.   
  179. int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始  
  180.   
  181. 的n个字符组成的字符串与s的大小  
  182.   
  183. int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当  
  184.   
  185. 前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串  
  186.   
  187. 的大小  
  188.   
  189. int compare(const char *s) const;  
  190.   
  191. int compare(int pos, int n,const char *s) const;  
  192.   
  193. int compare(int pos, int n,const char *s, int pos2) const;  
  194.   
  195. compare函数在>时返回1,<时返回-1,==时返回0    
  196.   
  197. string的子串:  
  198.   
  199. string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的  
  200.   
  201. 字符串string的交换:  
  202.   
  203. void swap(string &s2);    //交换当前字符串与s2的值  
  204.   
  205.    
  206.   
  207. string类的查找函数:  
  208.   
  209. int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置  
  210.   
  211. int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串  
  212.   
  213. 中的位置  
  214.   
  215. int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n  
  216.   
  217. 个字符在当前串中的位置  
  218.   
  219. int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前  
  220.   
  221. 串中的位置  
  222.   
  223. //查找成功时返回所在位置,失败返回string::npos的值  
  224.   
  225. int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前  
  226.   
  227. 串中的位置  
  228.   
  229. int rfind(const char *s, int pos = npos) const;  
  230.   
  231. int rfind(const char *s, int pos, int n = npos) const;  
  232.   
  233. int rfind(const string &s,int pos = npos) const;  
  234.   
  235. //从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成  
  236.   
  237. 功返回所在位置,失败时返回string::npos的值  
  238.   
  239. int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出  
  240.   
  241. 现的位置  
  242.   
  243. int find_first_of(const char *s, int pos = 0) const;  
  244.   
  245. int find_first_of(const char *s, int pos, int n) const;  
  246.   
  247. int find_first_of(const string &s,int pos = 0) const;  
  248.   
  249. //从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找  
  250.   
  251. 失败返回  
  252.   
  253. string::npos  
  254.   
  255. int find_first_not_of(char c, int pos = 0) const;  
  256.   
  257. int find_first_not_of(const char *s, int pos = 0) const;  
  258.   
  259. int find_first_not_of(const char *s, int pos,int n) const;  
  260.   
  261. int find_first_not_of(const string &s,int pos = 0) const;  
  262.   
  263. //从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos  
  264.   
  265. int find_last_of(char c, int pos = npos) const;  
  266.   
  267. int find_last_of(const char *s, int pos = npos) const;  
  268.   
  269. int find_last_of(const char *s, int pos, int n = npos) const;  
  270.   
  271. int find_last_of(const string &s,int pos = npos) const;  
  272.   
  273. int find_last_not_of(char c, int pos = npos) const;  
  274.   
  275. int find_last_not_of(const char *s, int pos = npos) const;  
  276.   
  277. int find_last_not_of(const char *s, int pos,  int n) const;  
  278.   
  279. int find_last_not_of(const string &s,int pos = npos) const;  
  280.   
  281. //find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只  
  282.   
  283. 不过是从后向前查找  
  284.   
  285.    
  286.   
  287. string类的替换函数:  
  288.   
  289. string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然  
  290.   
  291. 后在p0处插入串s  
  292.   
  293. string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字  
  294.   
  295. 符,然后在p0处插入字符串s的前n个字符  
  296.   
  297. string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,  
  298.   
  299. 然后在p0处插入串s  
  300.   
  301. string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开  
  302.   
  303. 始的n0个字符,然后在p0处插入串s中从pos开始的n个字符  
  304.   
  305. string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后  
  306.   
  307. 在p0处插入n个字符c  
  308.   
  309. string &replace(iterator first0, iterator last0,const char *s);//把[first0  
  310.   
  311. ,last0)之间的部分替换为字符串s  
  312.   
  313. string &replace(iterator first0, iterator last0,const char *s, int n);//把  
  314.   
  315. [first0,last0)之间的部分替换为s的前n个字符  
  316.   
  317. string &replace(iterator first0, iterator last0,const string &s);//把  
  318.   
  319. [first0,last0)之间的部分替换为串s  
  320.   
  321. string &replace(iterator first0, iterator last0,int n, char c);//把[first0  
  322.   
  323. ,last0)之间的部分替换为n个字符c  
  324.   
  325. string &replace(iterator first0, iterator last0,const_iterator first,   
  326.   
  327. const_iteratorlast);//把[first0,last0)之间的部分替换成[first,last)之间  
  328.   
  329. 的字符串string类的插入函:  
  330.   
  331. string &insert(int p0, const char *s);  
  332.   
  333. string &insert(int p0, const char *s, int n);  
  334.   
  335. string &insert(int p0,const string &s);  
  336.   
  337. string &insert(int p0,const string &s, int pos, int n);  
  338.   
  339. //前4个函数在p0位置插入字符串s中pos开始的前n个字符  
  340.   
  341. string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c  
  342.   
  343. iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的  
  344.   
  345. 位置  
  346.   
  347. void insert(iterator it, const_iterator first, const_iterator last);//在it  
  348.   
  349. 处插入[first,last)之间的字符  
  350.   
  351. void insert(iterator it, int n, char c);//在it处插入n个字符c  
  352.   
  353.    
  354.   
  355. string类的删除函数  
  356.   
  357. iterator erase(iterator first, iterator last);//删除[first,last)之间的所  
  358.   
  359. 有字符,返回删除后迭代器的位置  
  360.   
  361. iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置  
  362.   
  363. string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改  
  364.   
  365. 后的字符串  
  366. 最后

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

望采纳

0
潘晨皓
潘晨皓
高级天翼
高级天翼

666

(我只知道一点,都被他们说完了)

0
我要回答