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