深入C++ string.find()函数的用法总结_MsSql

#include <string>
#include <iostream>
using namespace std;
void main()
{

复制代码 代码如下:

 ////find函数返回类型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函数 返回jk 在s 中的下标位置
position = s.find("jk");
 if (position != s.npos)  //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295,
 {
  cout << "position is : " << position << endl;
 }
 else
 {
  cout << "Not found the flag" + flag;
 } 

复制代码 代码如下:

//find 函数 返回flag 中任意字符 在s 中第一次出现的下标位置
 flag = "c";
 position = s.find_first_of(flag);
 cout << "s.find_first_of(flag) is : " << position << endl;

复制代码 代码如下:

 //从字符串s 下标5开始,查找字符串b ,返回b 在s 中的下标
 position=s.find("b",5);
 cout<<"s.find(b,5) is : "<<position<<endl;

复制代码 代码如下:

//查找s 中flag 出现的所有位置。
 flag="a";
 position=0;
 int i=1;
 while((position=s.find_first_of(flag,position))!=string::npos)
 {
  //position=s.find_first_of(flag,position);
  cout<<"position  "<<i<<" : "<<position<<endl;
  position++;
  i++;
 }

复制代码 代码如下:

 //查找flag 中与s 第一个不匹配的位置
 flag="acb12389efgxyz789";
 position=flag.find_first_not_of (s);
 cout<<"flag.find_first_not_of (s) :"<<position<<endl;

复制代码 代码如下:

 //反向查找,flag 在s 中最后出现的位置
 flag="3";
 position=s.rfind (flag);
 cout<<"s.rfind (flag) :"<<position<<endl;
}

说明:
1.如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。
s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。
2.如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。

时间: 2024-07-30 12:44:44

深入C++ string.find()函数的用法总结_MsSql的相关文章

深入C++ string.find()函数的用法总结

#include <string> #include <iostream> using namespace std; void main() { 复制代码 代码如下:  ////find函数返回类型 size_type string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i"); string flag; string::size_type position; //find 函数 返回jk 在s 中的下标位置 positi

如何使php文件与html码更好的分离--eval函数的用法

函数 如何使php文件与html码更好的分离             ------eval函数的用法             大家都知道,php是一种服务器端的内嵌html式的脚本编程语言.可是按照内嵌html方式来作一网站的话,代码很快就变得庞大而且不可控制.如何才能使php代码与html分离,做出类似dw的lib(模板)而使得页面更加容易修改并且代码容易维护呢?后来,看了很多文章,说phplib可以实现,随手看了几页,觉得头晕脑涨,顿时没有了看下去的欲望(那位大虾如果有此心得,敬请不吝赐教,

Java中split()函数和trim()函数的用法

split()函数是根据参数如",", "-", " "等, 分割String字符串, 返回一个String的数组(String[]), 可以通过索引指定元素. 如果未找到, 则返回整个String字符串, 作为String数组(String[])的第0个元素. trim()函数是去掉String字符串的首尾空格; 代码: /** * @author Spike * @time 2014.4.25 */ public class Split {

php源码分析之DZX1.5随机数函数random用法

  本文实例讲述了php源码分析之DZX1.5随机数函数random用法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?php /** * @param int $length: 随机数长度 * @param int $numeric: 0或非0,其中0表示随机数由全数字组成,非0表示随机数由全字母组成 * @return string: 返回生成的随机数 */ function random($l

php源码分析之DZX1.5加密解密函数authcode用法

  本文实例讲述了php源码分析之DZX1.5加密解密函数authcode用法.分享给大家供大家参考.具体分析如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 <?php $authkey

php源码分析之DZX1.5字符串截断函数cutstr用法

  本文实例讲述了php源码分析之DZX1.5字符串截断函数cutstr用法.分享给大家供大家参考.具体分析如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 <?php /** * 函数来源DZX1.5,文件所在 /source/function/func

javascript中match函数的用法小结

 本篇文章主要是对javascript中match函数的用法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助 javascript中的match函数是使用正则表达式对字符串进行查找,并将查找的结果作为数组返回,在实际开发中非常的有用,使用方法如下:  stringObj.match(rgExp)  其中stringObj是必选项.对其进行查找的 String 对象或字符串文字.  rgExp是必选项.为包含正则表达式模式和可用标志的正则表达式对象.也可以是包含正则表达式模式和可

GO语言延迟函数defer用法分析_Golang

本文实例讲述了GO语言延迟函数defer用法.分享给大家供大家参考.具体分析如下: defer 在声明时不会立即执行,而是在函数 return 后,再按照 FILO (先进后出)的原则依次执行每一个 defer,一般用于异常处理.释放资源.清理数据.记录日志等.这有点像面向对象语言的析构函数,优雅又简洁,是 Golang 的亮点之一. 代码1:了解 defer 的执行顺序 复制代码 代码如下: package main import "fmt" func fn(n int) int {

php图像处理函数imagecopyresampled用法详解_php技巧

本文实例讲述了php图像处理函数imagecopyresampled用法.分享给大家供大家参考,具体如下: 语法 复制代码 代码如下: bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h ) 参数 dst_i