• 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++ 字符串函数用法举例的更多相关文章

  1. 【转】awk 里的substr函数用法举例

    awk 里的substr函数用法举例: 要截取的内容:2007-08-04 04:45:03.084 - SuccessfulTradeResult(status: 1, currencyPair: ...

  2. Mysql截取和拆分字符串函数用法

    Mysql截取和拆分字符串函数用法 截取字符串函数: SUBSTRING(commentid,9) 意思是:从第9个字符开始截取到最后.SUBSTRING的参数有三个,最后一个是截取的长度,默认是到结 ...

  3. PostgreSQL 的日期函数用法举例

    最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考. 一 取当前日期的函数 --取当前时间skytf=> select no ...

  4. Oracle中的4大空值处理函数用法举例

    nvl(exp1,exp2):                           如果exp1为空,则返回exp2:否则返回exp1nvl2(exp1,exp2,exp3):             ...

  5. iframe调用父页面函数用法举例

    iframe如何调用父页面函数. window.parent.xxxxx();//xxxxx()代表父页面方法具体列子如下,其中包括easyUI的右键和单击事件parent.jspbody部分代码 & ...

  6. java字符串函数用法汇总

    替换字符串中的字符 例如有如下x的字符串 String x = "[kllkklk\kk\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相 ...

  7. PHP 语法字符串函数 strcmp、strlen 使用及实现

    说明 这里基于 php7.2.5 进行测试,php7 之后内部结构变化应该不是太大,但与 php5.X 有差别. 函数分类 用户自定义函数 say(); function say() { echo & ...

  8. c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例

    c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...

  9. Python中用format函数格式化字符串的用法

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下   自python2. ...

随机推荐

  1. 【每日scrum】NO.7

    Yesterday:学习和设计路线的编程 Today:编写代码 Problem:.在设计查询参观路线的时候,整个逻辑特别的混乱,设想了各种树,图以及网的遍历问题,但经过多次与同学的交流以及网上的查询资 ...

  2. iOS多线程自定义operation加载图片 不重复下载图片

    摘要:1:ios通过抽象类NSOperation封装了gcd,让ios的多线程变得更为简单易用:   2:耗时的操作交给子线程来完成,主线程负责ui的处理,提示用户的体验   2:自定义operati ...

  3. android 通过工具抓包

    工具: 1.tcpdump :http://www.strazzere.com/android/tcpdump 2.wireshark:http://pan.baidu.com/s/1geooiav ...

  4. 6、android开发中遇到的bug整理

    1.使用actionProvider时出现的问题 bug复现: 解决方案: //import android.support.v4.view.ActionProvider; import androi ...

  5. python-面向对象(指数对象举例)

    class Index(object): def __init__(self,index_name,index_code,closePrice_yesterday,closePrice_today): ...

  6. UVALive - 6571 It Can Be Arranged 最大流

    题目链接: http://acm.hust.edu.cn/vjudge/problem/48415 It Can Be Arranged Time Limit: 3000MS 问题描述 Every y ...

  7. Poj 1050 分类: Translation Mode 2014-04-04 09:31 103人阅读 评论(0) 收藏

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39058   Accepted: 20629 Desc ...

  8. matrix_world_final_2011

    C  http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98613#problem/C 题意:输入16进制的n*m矩阵,其在二进制表示下有6种 ...

  9. nginx+php+flight 构建RESTFul API

    配置: Nginx: conf目录下nginx.conf配置文件. 第44行改为:root   D:/wwwroot/www; 第45行改为:index  index.html index.htm i ...

  10. Sencha Touch xtype对应的class

    Sencha Touch 2的有效xtype xtype Class ----------------- --------------------- actionsheet Ext.ActionShe ...