C++ string类中的字符串查找
来源:互联网 发布:淘宝笔记本排行榜 编辑:程序博客网 时间:2024/06/04 01:21
C++ string类中的字符串查找
类string提供了大量查找功能和搜索功能,其中比较常用的查找和搜索函数是find()函数、
find_first_not_of()函数、find_first_of()函数、find_last_not_of()函数、find_last_of()函数、rfind()等。
find()函数的语法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回
该位置基于0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在
字符串中出现的位置并返回该位置基于0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的
字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串并且返回
该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
find()函数的功能是从std::string对象的头部顺序找目标值,如果找到返回该目标值出现的位置,如果没有在
字符串对象中找到目标对象,返回值为-1。
rfind()函数的语法如下所示:
(1) size_type rfind(E c, size_type pos = npos) const;用来反向查找单个字符在字符串中出现的位置
并返回该位置基于0的索引值。
(2) size_type rfind(const E *s, size_type pos = npos) const;用来反向查找以空字符结尾的字符数组
在字符串中出现的位置并返回该位置基于0索引值。
(3) size_type rfind(const E *s, size_type pos, size_type n = npos) const;用来反向查找以空字符
结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type rfind(const basic_string &str, size_type pos = npos) const;用来反向查找字符串并且
返回出现该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
rfind()函数的功能是从std::sring对象的尾部反向查找目标值,如果找到返回该目标值出现的位置,如果没有
在字符串对象中找到目标对象,返回值为-1。
find_first_not_of()函数的常见语法如下所示:
size_type find_first_not_of(E c, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos, size_type n) const;
size_type find_first_not_of(const basic_string &str, size_type pos = 0) const;
该函数的功能是在string对象中查找对象,如果在string出现了完全不匹配的字符,字符串或以空字符结尾的
字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。
find_first_of()函数的常见语法如下所示:
size_t find_first_of( const string& str, size_t pos = 0 ) const;
size_t find_first_of( const char* s, size_t pos, size_t n ) const;
size_t find_first_of( const char* s, size_t pos = 0 ) const;
size_t find_first_of( char c, size_t pos = 0 ) const;
该函数的功能是在string对象中查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结尾
的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。只要在string对象中出现了
匹配对象,立即返回位置。
find_last_not_of()函数的常见语法如下所示:
size_t find_last_not_of( const string& str, size_t pos = npos ) const;
size_t find_last_not_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_not_of( const char* s, size_t pos = npos ) const;
size_t find_last_not_of( char c, size_t pos = npos ) const;
该函数的功能是在string对象中反向查找对象,如果在string出现了任意完全不匹配的字符,字符串或以空
字符结尾的字符数组时,系统显示第一次完全不匹配时出现的位置。如果定义了pos,从pos反向开始搜索。只要
在string对象中出现了完全不匹配的对象,立即返回位置值。
find_last_of()函数的常见语法如下所示:
size_t find_last_of( const string& str, size_t pos = npos ) const;
size_t find_last_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_of( const char* s, size_t pos = npos ) const;
size_t find_last_of( char c, size_t pos = npos ) const;
该函数的共是在string对象中反向查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结
尾的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始反向搜索。只要在string对象
中出现了匹配的对象,则立即返回位置值。
类string提供了大量查找功能和搜索功能,其中比较常用的查找和搜索函数是find()函数、
find_first_not_of()函数、find_first_of()函数、find_last_not_of()函数、find_last_of()函数、rfind()等。
find()函数的语法如下所示:
(1) size_type find(E c, size_type pos = npos) const;用来查找单个字符在字符串中出现的位置并返回
该位置基于0的索引值。
(2) size_type find(const E *s, size_type pos = npos) const;用来查找以空字符结尾的字符数组在
字符串中出现的位置并返回该位置基于0索引值。
(3) size_type find(const E *s, size_type pos, size_type n = npos) const;用来查找以空字符结尾的
字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type find(const basic_string &str, size_type pos = npos) const;用来查找字符串并且返回
该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
find()函数的功能是从std::string对象的头部顺序找目标值,如果找到返回该目标值出现的位置,如果没有在
字符串对象中找到目标对象,返回值为-1。
rfind()函数的语法如下所示:
(1) size_type rfind(E c, size_type pos = npos) const;用来反向查找单个字符在字符串中出现的位置
并返回该位置基于0的索引值。
(2) size_type rfind(const E *s, size_type pos = npos) const;用来反向查找以空字符结尾的字符数组
在字符串中出现的位置并返回该位置基于0索引值。
(3) size_type rfind(const E *s, size_type pos, size_type n = npos) const;用来反向查找以空字符
结尾的字符数组在字符串中出现的位置并返回该位置基于0索引值,它是从npos开始查找的。
(4) size_type rfind(const basic_string &str, size_type pos = npos) const;用来反向查找字符串并且
返回出现该搜索的字符串的基于0索引值的位置值,它是从npos开始查找的。
rfind()函数的功能是从std::sring对象的尾部反向查找目标值,如果找到返回该目标值出现的位置,如果没有
在字符串对象中找到目标对象,返回值为-1。
find_first_not_of()函数的常见语法如下所示:
size_type find_first_not_of(E c, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos = 0) const;
size_type find_first_not_of(const E *s, size_type pos, size_type n) const;
size_type find_first_not_of(const basic_string &str, size_type pos = 0) const;
该函数的功能是在string对象中查找对象,如果在string出现了完全不匹配的字符,字符串或以空字符结尾的
字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。
find_first_of()函数的常见语法如下所示:
size_t find_first_of( const string& str, size_t pos = 0 ) const;
size_t find_first_of( const char* s, size_t pos, size_t n ) const;
size_t find_first_of( const char* s, size_t pos = 0 ) const;
size_t find_first_of( char c, size_t pos = 0 ) const;
该函数的功能是在string对象中查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结尾
的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始搜索。只要在string对象中出现了
匹配对象,立即返回位置。
find_last_not_of()函数的常见语法如下所示:
size_t find_last_not_of( const string& str, size_t pos = npos ) const;
size_t find_last_not_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_not_of( const char* s, size_t pos = npos ) const;
size_t find_last_not_of( char c, size_t pos = npos ) const;
该函数的功能是在string对象中反向查找对象,如果在string出现了任意完全不匹配的字符,字符串或以空
字符结尾的字符数组时,系统显示第一次完全不匹配时出现的位置。如果定义了pos,从pos反向开始搜索。只要
在string对象中出现了完全不匹配的对象,立即返回位置值。
find_last_of()函数的常见语法如下所示:
size_t find_last_of( const string& str, size_t pos = npos ) const;
size_t find_last_of( const char* s, size_t pos, size_t n ) const;
size_t find_last_of( const char* s, size_t pos = npos ) const;
size_t find_last_of( char c, size_t pos = npos ) const;
该函数的共是在string对象中反向查找对象,如果在string出现了任意完全匹配的字符,字符串或以空字符结
尾的字符数组时,系统显示第一次出现这种情形的位置。如果定义了pos,从pos开始反向搜索。只要在string对象
中出现了匹配的对象,则立即返回位置值。
字符串查找实例find_first_of()
运行结果为:
The string to search is 'Heartbeat'
Element in 'abcde' found at position 1
Element in 'abcde' found at position 2
Element in 'aeiou' found at position 6
Element in 'aeiou' found at position 1
'e' found at position 6
0 0
- C++ string类中的字符串查找
- C++ string类中的字符串查找
- string类字符串中的两个查找字符函数
- C++【String类】String查找单个字符,查找字符串的函数实现
- [C++]查找文件中的一串字符串
- java基础之String类查找字符串
- String字符串查找操作
- 字符串查找String.IndexOf
- C:strstr 查找指定字符串在源字符串中的位置
- Java中的字符串string类
- string类和c字符串
- [C++]string类的查找函数
- C语言字符串函数和C++string类中的部分函数的使用
- C/C++ string库(string.h)提供了几个字符串查找函数
- C/C++ string库(string.h)提供了几个字符串查找函数
- string的查找字符串操作
- C++ string 字符串查找匹配
- 查找字符串中的数字
- 集电极开路
- Android自定义开关
- 数据结构:单链表(四)
- C# WinForm控件之Dock先后顺序调整
- VS2010使用大漠插件
- C++ string类中的字符串查找
- 对AsyncTask的深入了解
- 用Java实现计算器
- spring7
- C++远征之封装篇——常对象成员、常成员函数
- PAT-A-1038. Recover the Smallest Number (30)
- MySQL的统计函数
- java -- 函数/方法
- 转载:Segmentation fault到底是何方妖孽