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. ...
随机推荐
- C语言编写的随机产生四则运算测试题
题目:编写一个四则运算测试题的程序,要求每道题都要随机产生 解题思路: 1.编写测试题,且为30道,就要用到循环函数,因此想到用for()函数 2.随机产生两个数,就想到用rand()函数. 注:1. ...
- saltstack知识点
1.salt '*' sys.doc 显示minion设备上支持的命令 2.salt-call 可以显示比salt更多的信息,可以用来调试,检查,如果需要更详细的信息使用 salt-call -l ...
- SpringMVC处理脚本,SQL注入问题
SpringMVC处理脚本,SQL注入问题(写的不好勿喷,互相学习) 使用 Filter 来过滤浏览器发出的请求,对每个URI参数请求过滤些关键字,替换成安全的字符.所有请求的 getParamete ...
- Node.js 学习(二) 创建第一个应用
如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi. 从这个角度看,整个"接收 HTTP 请求并提供 ...
- android 图片缩放抗锯齿
之前用的时候只设置了antialias属性,其实要设置两个flag才行 paint.setFlags(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG); ...
- Retry Pattern
Retry Pattern https://msdn.microsoft.com/en-us/library/dn589788.aspx https://msdn.microsoft.com/en-u ...
- Leetcode#146 LRU Cache
原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...
- Linux安装Flash player
在线自动更新:Flash Player Update(需java) ===================== 下载得到install_flash_player_11_linux.x86_64.tar ...
- javascript设计模式--状态模式(State)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MYSQL注入天书之后记
后记 对于工具的看法: 我之所以在每个例子中只写了几个示例,是因为我希望你能通过这一两个示例举一反三将其他的列出来.如果让我来完成每一次完整的注入,应该在知道原理的情况下,必然使用工具或者自己写代码实 ...