写个小工具函数

#include <iostream>
#include <vector>
using namespace std; namespace strtool
{
string trim(const string& str)
{
string::size_type pos = str.find_first_not_of(' ');
if (pos == string::npos)
{
return str;
}
string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != string::npos)
{
return str.substr(pos, pos2 - pos + );
}
return str.substr(pos);
} int split(const string& str, vector<string>& ret_, string sep = ",")
{
if (str.empty())
{
return ;
} string tmp;
string::size_type pos_begin = str.find_first_not_of(sep);
string::size_type comma_pos = ; while (pos_begin != string::npos)
{
comma_pos = str.find(sep, pos_begin);
if (comma_pos != string::npos)
{
tmp = str.substr(pos_begin, comma_pos - pos_begin);
pos_begin = comma_pos + sep.length();
}
else
{
tmp = str.substr(pos_begin);
pos_begin = comma_pos;
} if (!tmp.empty())
{
ret_.push_back(tmp);
tmp.clear();
}
}
return ;
} string replace(const string& str, const string& src, const string& dest)
{
string ret; string::size_type pos_begin = ;
string::size_type pos = str.find(src);
while (pos != string::npos)
{
cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";
ret.append(str.data() + pos_begin, pos - pos_begin);
ret += dest;
pos_begin = pos + ;
pos = str.find(src, pos_begin);
}
if (pos_begin < str.length())
{
ret.append(str.begin() + pos_begin, str.end());
}
return ret;
} } int main(int argc, char* argv[])
{
cout << strtool::trim(" nihao ") <<"\n"; vector<string> vt;
strtool::split(",o h,,,nice,,,,,,,", vt);
for (size_t i = ; i < vt.size(); ++ i)
{
cout <<"out:" << vt[i] <<"\n";
} string ret = strtool::replace("xxAxxxAxxAxx", "A", "B");
cout <<"replace:" << ret <<"\n";
return ;
}

C++ 使用STL string 实现的split,trim,replace-修订的更多相关文章

  1. String类的split方法以及StringTokenizer

    split方法可以根据指定的表达式regex将一个字符串分割成一个子字符串数组. 它的参数有两种形式,也即:split(String regex)和split(String regex, int li ...

  2. C++ STL string对象操作汇总

    string对象 C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,显得十分不便.C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为 ...

  3. 【Java面试题】17 如何把一个逗号分隔的字符串转换为数组? 关于String类中split方法的使用,超级详细!!!

    split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回. stringObj.split([separator],[limit])参数:stringObj   必选项.要被分解的 ...

  4. 深入剖析 linux GCC 4.4 的 STL string

    转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...

  5. C#入门篇6-3:字符串操作 string的ToString() Split()和Copy()方法

    //ToString()方法 public static void OutPut() { //字符型转换 转为字符串 Console.WriteLine(.ToString("n" ...

  6. 格式字符串分配stl::string

    代码非常easy,不解释,直接在代码: #include <cstdio> #include <cstdarg> #include <iostream> using ...

  7. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

  8. new String(request.getParameter("userID").trim().getBytes("8859_1"))的含义是什么?

    new String(request.getParameter("userID").trim().getBytes("8859_1")) request.get ...

  9. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

随机推荐

  1. 《图解设计模式》读书笔记2-1 Template Method模式

    目录 模板方法模式 类图 思想: 模板方法模式 在父类中定义流程,在子类中实现具体的方法. 类图 代码 //抽象类 public abstract class AbstractDisplay { pu ...

  2. Cookie 记录最后访问时间

    package cn.gs.ly.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.D ...

  3. IDEA activate-power-mode插件

    一 下载activate-power-mode插件 方法1:插件库下载: 地址:http://plugins.jetbrains.com/plugin/8330-activate-power-mode ...

  4. Learn Python the hard way, ex40 字典,可爱的字典

    #!/usr/bin/python #coding:utf-8 cities ={'CA':'sf','MI':'dt','FL':'je'} #创建字典 cities['NY']='ny' #增加新 ...

  5. 《JAVA设计模式》之中介者模式(Mediator)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述调停者(Mediator)模式的: 调停者模式是对象的行为模式.调停者模式包装了一系列对象相互作用的方式,使得这些对象不必相互明显引用.从 ...

  6. 《剑指offer》面试题7 用两个栈实现队列 Java版

    书中方法:队列是先进先出的,栈是先进后出的,试想把一串数压入A栈,接着一个个出栈并压入B栈,便会完成"头在下"到"头在上"的转变.B栈内还有元素时,直接出栈表示 ...

  7. python读取ini文件时,特殊字符的读取

    前言: 使用python在读取配置文件时,由于配置文件中存在特殊字符,读取时出现了以下错误: configparser.InterpolationSyntaxError: '%' must be fo ...

  8. python学习第三天格式化输出%s %d

    编程语言为什么要格式化输出吗,一般print()就够了,有些复杂的格式输出比较麻烦,用格式化输出更加高效, info=""" ---------------------- ...

  9. JVM(12)之 可视化分析工具

    开发十年,就只剩下这套架构体系了! >>>   经过前几篇博文对堆内存以及垃圾收集机制的学习,相信小伙伴们已经建立了一套比较完整的理论体系!本篇博客就根据已有的理论知识,通过可视化工 ...

  10. JS-01 书写规范

    此部分内容整理自私教指导和自我体会:(持续更新...) 1.运算符左右两边留空格 (webstorm快捷键ctrl+alt+l): 2.判断值是否相等尽量用“===” 严格等于 : 3.编程中,可有可 ...