• 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. 如何分离数据库 (SQL Server Management Studio)

    在 SQL Server Management Studio 对象资源管理器中,连接到 SQL Server 数据库引擎的实例上,再展开该实例. 展开“数据库”,并选择要分离的用户数据库的名称. 分离 ...

  2. ExtJS登陆页面涉及到的几个问题

    1.如何在文本框中增加提示信息      输入框中无法直接使用tooltip,需要使用单独的代码 var tip = Ext.create('Ext.tip.ToolTip', { target : ...

  3. Json Serialize 忽略特定属性

    Json Serialize 忽略特定属性 Json Serialize SerializeFilter 忽略特定属性 key words:Json Serialize jackson fastjso ...

  4. 1-Highcharts 3D图之普通3D柱状图与带空值

    <!DOCTYPE> <html lang='en'> <head> <title>1-Highcharts 3D图之普通3D柱状图与带空值</t ...

  5. 3-Highcharts曲线图之显示点值折线图

    直接上代码  根据代码注释讲解 <!DOCTYPE> <html lang='en'> <head> <title>3-Highcharts曲线图之显示 ...

  6. 无法解决 equal to 运算中 &quot;Chinese_PRC_CI_AS&quot; 和 &quot;SQL_Latin1_General_CP1_CI_AS&quot; 之间的排序规则冲突。

    什么是排序规则(collation) 关于SQL Server的排序规则,估计大家都不陌生,在创建数据库时我们经常要选择一种排序规则(conllation),一般我们会留意到每一种语言的排序规则都有许 ...

  7. ios 环境配置网址

    http://blog.csdn.net/cwb1128/article/details/18019751

  8. iOS常见各种ID

    //CFUUID CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault); NSString *cfuuidString = (NSString*)C ...

  9. java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

    org.springframework.dao.TransientDataAccessResourceException: ### Error updating database. Cause: ja ...

  10. [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY

    [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY 发布者:sqqdugdu 时间:10-06 阅读数:2117 测试环境:RHEL 6.1,SecureCRT 5 ...