字符串相关操作笔记

笔记

#include <string>
#include <cstdio>
using namespace std;
int main(){
    char buf[101];//申请一个字符
    //scanf("%s",buf);//只能读取一个单词
    fgets(buf,100,stdin); //读取一整行,包括换行符
    string str = buf; //用一个字符数组生成一个c++风格的字符串
//    printf("length of str = %u\n",str.size());
    str.erase(5);//删除下标为5的字符 str.erase(str,size()-1)
    str.clear();//清空操作
    str = str + "world"; // +, 在C++风格的字符串中表示连接操作
    printf("str = %s\n",str.c_str()); //将C++风格的字符串转换成字符数组

//    for(unsigned int i=0;i<str.size();++i){
//        printf("%c\n",str[i]); //打印字符
//    }  
//另一种方法
//    for(string::iterator it =str.begin();it != str.end();++it)
//        //++it 更改迭代器的指向,到下一个元素
//        printf("%c\n",*it);//*it访问地址元素

/*
 * find 查找字符或子串 ,如果找到了,返回匹配内容的起点下标;否则,返回string::npos
 */
}
#include <cstdio>
#include <string>
using namespace std;
int main(){
    string str = "howareyou";
    int pos = str.find("arre");
    //判断一下are有没有在str当中出现过
    if(pos != string::npos){
        //有匹配
        printf("Found,pos = %d\n",pos);
    }
    else{
        //没有匹配项
        printf("Not Found\n");
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值