string的常见用法

string的使用

c语言中一般使用字符数组来存放字符串,但是这样使用起来容易出现一些错误。为了避免使用时候的麻烦和错误,c++在stl中加string类,对常用的功能进行了封装。

string的定义

1.头文件包含string

# include <string>
# include <iostream>
using namespace std;

2.定义和初始化

string str;
string str="abcd";

string内容访问

一般来说可以通过字符串数组方式访问string

通过下标访问

# include <iostream>
using namespace std;
int main(void)
{
    string str="abcd";
    for(int i=0;i<str.length();i++)
    {
        cout<<str[i]<<' '<<endl;
    }
    return 0;
}

输入输出整个字符串

# include <iostream>
using namespace std;
int main(void)
{
    string str;
    cin>>str;
    cout<<str<<endl;
    return 0;
}

通过迭代器访问

# include <iostream>
using namespace std;
int main(void)
{
    string str="abcde";
    for(string::iterator it=str.begin();it!=str.end();it++)
    {
        cout<<*it<<endl;
    }
    return 0;
}

string常见函数

字符串拼接

# include <iostream>
using namespace std;
int main(void)
{
   string str1="abc";
   string str2="def";
   string str3;
   str3=str1+str2;
   str2+=str1;
   cout<<str1<<' '<<str2<<' '<<str3<<endl;
   return 0;
}

字符串比较

按照字典的顺序来比较字符串

# include <iostream>
using namespace std;
int main(void)
{
   string str1="abc";
   string str2="def";
   if(str1>str2)
   cout<<"str1>str2"<<endl;
   else
   cout<<"str1<str2"<<endl;
   return 0;
}

获取字符串长度

size()和length()函数都是获取当前字符串的长度,区别不大

# include <iostream>
using namespace std;
int main(void)
{
   string str1="abc";
   string str2="def";
   cout<<str1.length()<<' '<<str2.size()<<endl;
   return 0;
}

插入字符串

insert(pos,string)
# include <iostream>
using namespace std;
int main(void)
{
   string str1="Hello";
   string str2="world";
   str1.insert(2,str2);
   cout<<str1<<endl;
   //outcome=Heworldllo
   return 0;
}
insert(it,it2,it3)

it是要插入的位置
it2,it3是要插入字符的首尾迭代器

# include <iostream>
using namespace std;
int main(void)
{
   string str1="Hello";
   string str2="world";
   str.insert(str1.begin()+2,str2.begin(),str2.end());
   //outcome=Heworldllo
   return 0;
}

删除单个元素或者区间元素

删除单个元素
# include <iostream>
using namespace std;
int main(void)
{
   string str="abcdef";
   str.erase(str.begin()+2);//删除第三个元素
   cout<<str<<endl;//abdef
   return 0;
}
删除区间元素

erase(first,lase),first为要删除区间的首迭代器,end是要删除区间的尾部迭代器

# include <iostream>
using namespace std;
int main(void)
{
   string str="abcdef";
   str.erase(str.begin(),str.begin()+2);
   cout<<str<<endl;//[str.begin(),str.begin()+2)区间的元素
   return 0;
}

erase(pos,length),pos删除开始的位置,而lenth是要删除的长度

# include <iostream>
using namespace std;
int main(void)
{
   string str="abcdef";
   str.erase(2,2);
   cout<<str<<endl;//"abef"
   return 0;
}

删除所有数据

# include <iostream>
using namespace std;
int main(void)
{
   string str="abcdef";
   str.clear();
   cout<<str.length()<<endl;//0
   return 0;
}

字符串切片substr

# include <iostream>
using namespace std;
int main(void)
{
   string str="abcdef";
   str.substr(0,4);//[0,4)
   cout<<str<<endl;//abcd
   return 0;
}

string::npos

string::npos是一个常数,其本身值为-1,但是由于是unsigned_int类型,因此实际上也可以认为是unsigned_int的最大值,string::npos是find函数失配的返回值

字符串匹配find

str.find(str2),str2是str的子字符串,返回str第一次出现str2的位置
str.find(str2,pos),str从pos位开始匹配str2,返回第一次出现位置的下标

# include <iostream>
using namespace std;
int main(void)
{
   string str="Thank you for your smile";
   string str2="you";
   string str3="me";
   if(str.find(str2)!=str::npos)
   cout<<str.find(str2);
   if(str.find(str2,7)!=str::npos)
   cout<<str.find(str2,7);
   if(str.find(str3)!=str::npos)
   cout<<str.find(str3);
   return 0;
}

字符串替换

str.replace(pos,len,str2),str从pos位置起,长度为len的范围,全部替换为str2
str.replace(it1,it2,str2),str迭代器[it1,it2)范围替换为str2

# include <iostream>
using namespace std;
int main(void)
{
   string str1="Hello World! I am coming!";
   string str2="me";
   string str3="you";
   str1.replace(0,2,str2);
   str1.replace(str1.begin(),str1.end(),str3);
   cout<<str1<<endl;
   str1.replace(0)
   return 0;
}

转载于:https://www.cnblogs.com/mengxiaoleng/p/11371470.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值