C++中string查找和取子串和整形转化
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查找和取子串和整形转化的更多相关文章
- 118、Java中String类之取字符串长度
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- C++string中用于查找的find系列函数浅析
总述: 以下所讲的所有的string查找函数,都有唯一的返回类型,那就是size_type,即一个无符号整数(按打印出来的算).若查找成功,返回按查找规则找到的第一个字符或子串的位置:若查找 ...
- C++中String字符串查找
在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...
- Swift 中 String 取下标及性能问题
Swift 中 String 取下标及性能问题 取下标 String String 用 String.Index 取下标(subscript)得到 Character,String.Index 要从 ...
- freemarker中的substring取子串
freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...
- freemarker中的substring取子串(十四)
freemarker中的substring取子串 1.substring取子串介绍 (1)表达式?substring(from,to) (2)当to为空时,默认的是字符串的长度 (3)from是第一个 ...
- java中String类学习
java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...
- c++中string类的详解
,<时返回-1,==时返回0 string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...
- c++中string类的具体解释
通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...
随机推荐
- win10删除导航栏文档等图标,去除快捷方式
去除快捷方式字样 找到[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer],在右侧窗格新建或修改名为“link” ...
- HTML5 中的新属性autocomplete="off"失效的解决方法(兼容firefox,IE,360)
因为业务需求,在写一个注册页面的时候,发现浏览器会自动填充此域名下已经保存的账号密码,给用户带来不便.加了HTML5 中的新属性autocomplete="off" ,但是并没有产 ...
- 关于spring AOP的学习
比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html
- SQL Server 索引(index) 和 视图(view) 的简单介绍和操作
--索引(index)和视图(view)-- --索引(index)----概述: 数据库中的索引类似于书籍的目录,他以指针形式包含了表中一列或几列组合的新顺序,实现表中数据库的逻辑排序.索引创建在数 ...
- 13 Balls Problem
今天讨论的是称球问题. No.3 13 balls problem You are given 13 balls. The odd ball may be either heavier or ligh ...
- VFP MSSOAPTOOKIT 使用SOAP Headers
.NET 有如下使用了自定义扩展HEADER来做验证 server.asmx代码 using System;using System.Collections.Generic;using System ...
- add number
// io.cpp #include <iostream> int readNumber() { std::cout << "Enter a number: &quo ...
- 《转》iOS音频视频初级开发
代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS ...
- linux默认网关的设置
linux装系统设IP,这应该是系统管理员的基本功,可是不同的网络结构有不同的ip设法,您知道吗? 1.一块网卡的情况 这个没啥好说的,估计地球人都知道:address,netmask,gatew ...
- JDBC步骤
总结JDBC五步骤: 1.Class.forName 加载驱动 2.conn = (Connection) DriverManager.getConnection(url, user, passwor ...