设为首页 加入收藏

TOP

c++之string类用法详细总结(二)
2015-11-21 00:58:20 来源: 作者: 【 】 浏览:5
Tags:string 用法 详细 总结
字符

8)size_tfind_last_not_of (const char* s, size_t pos = npos) const;

//在当前字符串的pos索引位置开始,查找最后一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串

#include 
  
   
#include 
   
     using namespace std; int main(void) { //0123456789012345678901234 string s(dog bird chicken bird cat); //字符串查找 cout << s.find(bird) << endl; //打印 cout << (int)s.find(pig) << endl; //打印-1 //字符查找 cout << s.find('i',7) << endl; //打印 //从字符串的末尾开始查找字符串 cout << s.rfind(bird) << endl; //打印 //从字符串的末尾开始查找字符 cout << s.rfind('i') << endl; //打印 //查找第个属于某子串的字符 cout << s.find_first_of(13r98) << endl; //找到字符r,打印 //查找第个不属于某字符串的字符 cout << s.find_first_not_of(dog bird 2006) << endl; //找到字符c,打印 //查找最后一个属于某子串的字符 cout << s.find_last_of(13r98) << endl; //字符r,打印 //查找最后一个不属于某字符串的字符 cout << s.find_last_not_of(tea) << endl; //字符c,打印 return 0; } 
   
  

\

字符串的比较

1)int compare (conststring& str) const;//将当前字符串与字符串str比较,

相等返回0,大于str返回1,小于str返回-1

2)int compare (size_tpos, size_t len, const char* s) const; //将当前字符串从

Pos索引位置开始的len个字符构成的字符串与字符串s比较,

相等返回0,大于str返回1,小于str返回-1

3)int compare (constchar* s) const; //直接将当前字符串与字符串s比较,

相等返回0

#include 
  
   
#include 
   
     using namespace std; int main(void) { string s1(abcdef); string s2(abc); cout << s1.compare(abcdef) << endl; //相等,打印 cout << s1.compare(s2) << endl; //s1 > s2,打印 cout << s1.compare(abyz) << endl; //s1 < abyz,打印-1 cout << s1.compare(0,3,s2) << endl; //s1的前个字符==s2,打印 return 0; } 
   
  
\
?
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++字符串操作二 下一篇c++ 重载运算与类型转换

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: