C/C++常用的字符串操作

1、字符串转整形  

int atoi(const char *str );

注意:输入是 C风格的字符数组,如果 是C++ string类型,输入可以调用 str.c_str()输入C风格字符串;

          如果输入中不全为数字类型字符,则转换会在第一个不是数字类型的字符处停止。

例如:

string str = "181.484";
int x = atoi(str.c_str());

上述例子中,x等于181;

2、取子字符串

string str = "giregjniuer";
string sub1 = str.substr(1); //取str的1号下标到最后位置为子串
string sub1 = str.substr(0,5); //取str的0号下标开始后的5个字符为子串

3、从字符串中查找字符串

(1)size_t find (const string& str, size_t pos = 0) const;  //查找对象--string类对象

(2)size_t find (const char* s, size_t pos = 0) const; //查找对象--字符串

(3)size_t find (const char* s, size_t pos, size_t n) const;  //查找对象--字符串的前n个字符

(4)size_t find (char c, size_t pos = 0) const;  //查找对象--字符

结果:找到 -- 返回 第一个字符的索引

     没找到--返回   string::npos

	string str = "qau ni ma b";
	int id1 = str.find("ni");
	int id2 = str.find("a",id1); // 从id1位置往后找,找到的下标是相对于整个字符串而不是id1开始
	int id3 = str.find("ni m",0,2); //第三个参数,表示要查找的字符串(第一个参数)的前几个字符

4、切割字符串

切割字符串可以通过find和substr来实现,也可以通过下面函数实现。

char* strtok (char* str,constchar* delimiters );

函数功能: 
  切割字符串,将str切分成一个个子串 
函数参数: 
  str:在第一次被调用的时间str是传入需要被切割字符串的首地址;在后面调用的时间传入NULL。 
  delimiters:表示切割字符串(字符串中每个字符都会 当作分割符)。 
函数返回值: 
  当s中的字符查找到末尾时,返回NULL; 
  如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针。

char buf[]="hello@boy@this@is@heima";
    char*temp = strtok(buf,"@");
    while(temp)
    {
        printf("%s ",temp);
        temp = strtok(NULL,"@");
    }

5、替换字符串

替换(replace)

语法:

  basic_string &replace( size_type index, size_type num, const basic_string &str );
  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
  size_type num2 );
  basic_string &replace( size_type index, size_type num, const char *str );
  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
  basic_string &replace( iterator start, iterator end, const basic_string &str );
  basic_string &replace( iterator start, iterator end, const char *str );
  basic_string &replace( iterator start, iterator end, const char *str, size_type num );
  basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函数:

  • 用str中的num个字符替换本字符串中的字符,从index开始
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
  • 用str中的num个字符(从index开始)替换本字符串中的字符
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
  • 用num2个ch字符替换本字符串中的字符,从index开始
  • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
  • 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
  • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.

例如,以下代码显示字符串"They say he carved it himself...find your soul-mate, Homer."

    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";

    s.replace( 32, s2.length(), s2 );


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值