1.string.find函数

 #include <iostream>
#include <string>
using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { string s;
string str;
char *p = "abc"; int index; getline(cin,s);
cout<<"s="<<s<<endl; getline(cin,str);
cout<<"str="<<str<<endl; index = s.find(str);
cout<<"char index="<<index<<endl; index = s.find(p);
cout<<"string index="<<index<<endl; return ;
}

2. 取子串

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std; //输入2015-10-22
int riqi(string str){
int i = ;
int year;
int month;
int day;
int i_start,i_end;
string tmp; i_start = str.find("-");//第一个'-'
tmp = str.substr(,i_start);//取从0下标开始的i_start个字符
year = atoi(tmp.c_str());//tmp.c_str()转化为C语言中字符串指针 i_end = str.rfind("-");//最后一个'-'
tmp = str.substr(i_start+,i_end - i_start);
month = atoi(tmp.c_str()); tmp = str.substr(i_end+);
day = atoi(tmp.c_str());
cout<<year<<" "<<month<<' '<<day<<endl;
//输出的结果为 2015 10 22
return ;
}

C++中string查找和取子串和整形转化的更多相关文章

  1. 118、Java中String类之取字符串长度

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. C++string中用于查找的find系列函数浅析

    总述:      以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...

  3. C++中String字符串查找

    在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...

  4. Swift 中 String 取下标及性能问题

    Swift 中 String 取下标及性能问题 取下标 String String 用 String.Index 取下标(subscript)得到 Character,String.Index 要从 ...

  5. freemarker中的substring取子串

    freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...

  6. freemarker中的substring取子串(十四)

    freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...

  7. java中String类学习

    java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...

  8. c++中string类的详解

    ,<时返回-1,==时返回0  string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...

  9. c++中string类的具体解释

    通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...

随机推荐

  1. 转:ServletContext,ActionContext,ServletActionContext

    ServletContext ServletContext从他的package信息可以看出,它是标准的JavaEE WebApplication类库 javax.servlet.ServletCont ...

  2. OC Runtime

    OC 是面向运行时的语言.Runtime就是系统在运行的时候的一些机制,其中最主要的是消息发送机制.OC语言与其他语言(如C语言)在函数(方法)的调用有很大的不同.C语言,函数的调用在编译的时候就已经 ...

  3. 1045 - Access denied for user 'root'@'localhost'(using password NO)解决方案

    原因为输入了不正确的密码,连接数据库时输入正确的密码即可! 另外一个原因是安装MySQL后MySQL产生了一个随机密码,而登录的时候没有将随机密码填上,解决方案见:http://www.cnblogs ...

  4. JS function document.onclick(){}报错Syntax error on token "function", delete this token

    JS function document.onclick(){}报错Syntax error on token "function", delete this token func ...

  5. EBS中配置OAF

    配置EBS的OAF作者 redqq 16:09 | 静态链接网址 | 最新回复 (0) | 引用 (0) | ERP学习 载jdev 9.03.5带Oracle Applications Extens ...

  6. sudo,linux 新建账号,并开通ssh登录

    新建账号需要root账号或sudo权限,sudo配置保存在/etc/sudoers文件. sudoers的配置格式一般为: root ALL=(ALL:ALL) ALL %sudo ALL=(ALL: ...

  7. UEditor演变的迷你版编辑器

    建立一个demo.html文件,首先在需要添加编辑器的地方加入以下代码,使用style可以设置编辑器的宽度和高度. <script type="text/plain" id= ...

  8. 二分查找-python

    约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...

  9. 01HTTP服务&AJAX编程

    HTTP服务&AJAX编程 一.服务器         1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类:              1.按系统分类:Lin ...

  10. 转:python中对list去重的多种方法

    对一个list中的新闻id进行去重,去重之后要保证顺序不变. 直观方法 最简单的思路就是: ids = [1,2,3,3,4,2,3,4,5,6,1] news_ids = [] for id in ...