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函数判断字符是不是字符或数字。

猜你喜欢

转载自blog.csdn.net/qq_33726635/article/details/106863491