c++ 字符串函数用法举例
- 1. substr()
- 2. replace()
- 例子:split()
字符串切割: substr
函数原型:
string substr ( size_t pos = , size_t n = npos ) const;
解释:抽取字符串中从pos(默认为0)开始,长度为npos的子字串
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello";
cout << s.substr() << endl;
cout << s.substr() << endl;
cout << s.substr(, ) << endl;
cout << s.substr(2, string::npos) << endl;
}
结果
hello
llo
ll
llo
注意:string 类将 npos 定义为保证大于任何有效下标的值
字符串替换:replace
函数原型:
basic string& replace(size_ type Pos1, size_type Num1, const type* Ptr );
basic string& replace(size_ type Pos1, size_type Num1,const string Str );
替换用Ptr(或str)替换字符串从Pos1开始的Num1个位置
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello";
string m = s.replace(, , "mmmmm");
cout << m << endl;
}
结果:hmmmmmlo
另
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "hello";
char a[] = "";
string m = s.replace(, , a);
cout << m << endl;
}
结果:h12345lo
举例
split()函数
#include <iostream>
#include <vector>
using namespace std; vector<string> split(const string &s, const string &delim)
{
vector<string> elems;
size_t pos = ;
size_t len = s.length();
size_t delim_len = delim.length();
if (delim_len == )
{
elems.push_back(s.substr(, len));
return elems;
}
while (pos < len)
{
int find_pos = s.find(delim, pos);
if (find_pos < )
{
elems.push_back(s.substr(pos, len-pos));
break;
}
elems.push_back(s.substr(pos, find_pos-pos));
pos = find_pos + delim_len;
}
return elems;
} int main()
{
vector<string> vec = split("hello^nihao^ohyi", "^");
for (int i = ; i < vec.size(); ++i)
cout << vec[i] << endl;
}
c++ 字符串函数用法举例的更多相关文章
- 【转】awk 里的substr函数用法举例
awk 里的substr函数用法举例: 要截取的内容:2007-08-04 04:45:03.084 - SuccessfulTradeResult(status: 1, currencyPair: ...
- Mysql截取和拆分字符串函数用法
Mysql截取和拆分字符串函数用法 截取字符串函数: SUBSTRING(commentid,9) 意思是:从第9个字符开始截取到最后.SUBSTRING的参数有三个,最后一个是截取的长度,默认是到结 ...
- PostgreSQL 的日期函数用法举例
最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考. 一 取当前日期的函数 --取当前时间skytf=> select no ...
- Oracle中的4大空值处理函数用法举例
nvl(exp1,exp2): 如果exp1为空,则返回exp2:否则返回exp1nvl2(exp1,exp2,exp3): ...
- iframe调用父页面函数用法举例
iframe如何调用父页面函数. window.parent.xxxxx();//xxxxx()代表父页面方法具体列子如下,其中包括easyUI的右键和单击事件parent.jspbody部分代码 & ...
- java字符串函数用法汇总
替换字符串中的字符 例如有如下x的字符串 String x = "[kllkklk\kk\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相 ...
- PHP 语法字符串函数 strcmp、strlen 使用及实现
说明 这里基于 php7.2.5 进行测试,php7 之后内部结构变化应该不是太大,但与 php5.X 有差别. 函数分类 用户自定义函数 say(); function say() { echo & ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
随机推荐
- 团队作业index
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312&quo ...
- asp.net动态添加GridView的模板列,并获取列值
一.动态添加模板列: 1.建立模板列样式: 说明:下边代码可以直接写在aspx文件中,也可以单独建立cs文件:另外,我没有写button.linkButton等控件,意思差不多,不过当需要添加事件时, ...
- myBatis自动生成相关代码文件配置(Maven)
pom.xml文件添加配置 <build> <finalName>generator</finalName> <plugins> <!-- mav ...
- 深入探讨Java类加载机制
一.前言 毕业至今,已经三年光景,平时基本接触不到关于类加载器的技术(工作上),相信很多同行在开始工作后很长一段时间,对于类的加载机制都没有深入的了解过,之前偶然的机会接触了相关的知识,感觉挺有意思, ...
- ELF文件格式
ELF--Linux下可执行文件格式 1.类型 常见的ELF格式文件包括: ...
- 【LCA】CodeForce #326 Div.2 E:Duff in the Army
C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...
- iOS 开发中的问题
错误提示: Your build settings specify a provisioning profile with the UUID “39642B69-0278-4265-8392-4B28 ...
- 引擎设计跟踪(九.14.2d) [翻译] shader的跨平台方案之2014
Origin: http://aras-p.info/blog/2014/03/28/cross-platform-shaders-in-2014/ 简译 translation: 作者在2012年写 ...
- asp.net中实现群发邮件功能
前段时间在帮老师开发的网站中需要用到一个群发邮件的功能,而自己之前学习cms系统的时候用的we7的群发邮件功能也有一些问题,于是乎便自己去网上查了一下资料,自己总结了一下,并且封装成了一个类,亲测有用 ...
- Web流程
Web阻塞加载,异步加载. 延迟执行,立即执行. 加载并执行,不要调用代码. 加载并执行时会调用代码,但是加载并执行时不要调用代码,否则会改变逻辑. 上面是Web流程,JS只是web的一部分.如果关注 ...