C++中常用的字符char判断与字符串string处理函数:isalnum、reverse、compare,+=

1、常用的字符char判断函数:
在这里插入图片描述
1.1、isalnum函数相当于:

if(s[i]<'0' || (s[i]>'9'&& s[i]<'a') || s[i]>'z'){
    return false;
}else{
	return true;

2、常用的字符串string处理函数:
2.1、compare比较函数:
相同返回0,不同返回<0或>0。

std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
  std::cout << str1 << " is not " << str2 << '\n'; //green apple is not red apple
if (str1.compare(6,5,"apple") == 0) // green apple is an apple
  std::cout << str1 << " is an apple\n";

2.2、反转字符串函数:reverse

string s = "abcde";
std::reverse(s.begin(),s.end());    // edcba

2.3、字符串拼接:+=

std::string name ("John");
std::string family ("Smith");
name += " K. ";         // c-string
name += family;         // string
name += 'A';           // character

不需要调用insert函数,在末尾插入单个字符:

name.insert(name.end(),'A');

总结:

1、不需要调用insert函数在末尾插入单个字符,直接用+=
2、isalnum函数判断字符是不是字符或数字。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值