博客&笔记(cpp字符串专题)

话说用博客云笔记可以卸载了2333

 

字符串の杂七杂八

 

字符串赋值:

char 型  

char qwq[105];
strcpy(qwq,"123");

string型

string qwq;
qwq="123";

 

计算长度  

 

char型        

char qwq[105];  
int len=strlen(qwq);

string 型 

string a;             
int len=a.length();

 

比较字符串大小

char型 strcmp("123", "456") 前者大返回1,前者小返回-1 相等返回0

string型 直接用>、< 、== 即

 

字符串与数字型数据的相互转换(以下函数都是针对char 型数组,用string会出错)

atoi()将字符串转换为整形

 char a[105]="123123";
 int len=atoi(a);
 cout<<len+1;

atof()将字符串转换为浮点形

 char a[105]="123123.6";
 printf("%f",atof(a)+0.1);

atol ()将字符串转换为长整型

 

 

 

 char a[105]="123123";
 int len=atol(a);
 cout<<len+1;

itoa(a,s,2) 将数字转换为字符 a为数字 s为字符 2为2进制

int a=123;
char s[105];
itoa(a,s,2);
cout<<s;

判断字符串中是否含有某子串:

#include<bits/stdc++.h>
using namespace std;

int main()
{   
    //定义两个相同部分为hello的字符串
    string str1="asdasdadhelloz,zxc";
    string str2="hello";
   
   //应该是一个类似迭代器的东西…还没有研究透
    string::size_type id = str1.find(str2);
    
    if(id != string::npos )
    {
        cout<<"字符串中含有"<<str2<<endl;
    }
    
    else 
    {
        cout<<"字符串中不含有"<<str2<<endl;
    }
}

输入带有空格的字符串:

char s[105];
gets(s);
cout<<s;

 

string类的字符串操作:

 

插入字符串

insert()函数可以在string字符串中置顶的位置插入另一个字符串,它的原型为:

string& insert (size_t pos, const string& str);
  • 1

看这个插入的格式我们就能猜想到,pos表示要插入的下标;str表示要插入的字符串,它可以是string变量,也可以是C风格的字符串。

看下面的代码:

#include <iostream>
#include <string>
using namespace std;
void main(){
    string s1, s2, s3;
    s1 = s2 = "1234567890";
    s3 = "aaa";
    s1.insert(5, s3);
    cout << s1 << endl;
    s2.insert(5, "bbb");
    cout << s2 << endl;


    system("pause");
}

 

运行结果:

 

12345aaa67890
12345bbb67890
请按任意键继续. . .

 

insert()函数的第一个参数有越界的可能,如果越界,则会产生运行时异常。捕获这个异常。

 


删除字符串

erase()函数可以删除string变量中的一个字符串,原型为:

string& erase (size_t pos = 0, size_t len = npos);
  • 1

pos 表示要删除的子字符串的起始下标,len表示要删除子字符串的长度。如果不指明len的话,那么直接删除pos到字符串结束处的所有字符(此时len =str.length-pos)。

示例代码如下:

#include <iostream>
#include <string>
using namespace std;
void main(){
    string s1, s2, s3;
    s1 = s2 = s3 = "1234567890";
    s2.erase(5);
    s3.erase(5, 3);
    cout<< s1 <<endl;
    cout<< s2 <<endl;
    cout<< s3 <<endl;
     system("pause");
}

 

运行结果:

 

1234567890
12345
1234590
请按任意键继续. . .

 

在 pos 参数没有越界的情况下, len 参数也可能会导致要删除的子字符串越界。但实际上这种情况不会发生,erase() 函数会从以下两个值中取出最小的一个作为待删除子字符串的长度:

 

  • len的值
  • 字符串长度减去 pos 的值。

简单的说,就是待删除字符串最多只能删除到字符串结尾。


提取字符串

substr()函数原型为:

string substr (size_t pos = 0, size_t len = npos) const;

pos为要提取的子字符串的起始下标,len为要提取的子字符串的长度.

#include <iostream>
#include <string>
using namespace std;
void  main(){
    string s1 = "first second third";
    string s2;
    s2 = s1.substr(6, 6);
    cout << s1 << endl;
    cout << s2 << endl;
    system("pause");
}

 

输出结果为:

 

first second third
second
请按任意键继续. . .

 

系统对 substr() 参数的处理和 erase() 类似:

 

  • 如果 pos 越界,会抛出异常;
  • 如果 len 越界,会提取从 pos 到字符串结尾处的所有字符。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值