如何在c++中使用字符串类

  介绍

本篇文章给大家分享的是有关如何在c++中使用字符串类,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强>赋值

,//方法1   ,string  str1 =,“woniu201";   ,//方法2   ,char * p =,“woniu201";   ,string  str2 =, p;

<>强遍历

,//方法1,使用下标   ,for  (int  i=0, i

<强>查找

, string  str5 =,“woniu201";   ,int  pos1 =, str5.find (“n",, 0),,,,//从位置0开始查找字符n在字符串str5中的位置   ,int  pos2 =, str5.find (“niu",, 0),,,//从位置0开始查找字符串妞妞在字符串str5中的位置   ,int  pos3 =, str5.find (“niu",, 0,, 2);//从位置0开始查找字符串牛前两个字符组成的字符串在str5中的位置

<强>截取

, string  str3 =,“woniu201";   ,string  str4 =, str3.substr(0 5);//返回从下标0开始的5个字符组成的字符串

<强>其他

,//字符串连接   ,string  str6 =,“woniu201";   ,string  str7 =,“hailuo201";   ,string  str8 =, str6  +, str7;   ,//判断是否相等   ,bool  bRet1 =, (str6 ==, str7);,//相等为真的,否则为假   ,//判断字符串是否为空   ,bool  bRet2 =, str6.empty ();   ,//字符串插入   ,string  str9 =, str6.insert (0,, str7);,//字符串str6的0位置插入字符串str7   ,//字符串交换   ,str6.swap (str7);//才能判断是否包含   字符串:才能size_type  idx =, str6.find (“woniu");   如果才能(idx ==,字符串::非营利组织)   {才能   ,,,cout  & lt; & lt;,“not  found", & lt; & lt;, endl;   以前,,}

以上就是如何在c++中使用字符串类,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

如何在c++中使用字符串类