#include <string>
#include "util.h" namespace strtool{ std::string trim(const std::string& str) {
std::string::size_type pos = str.find_first_not_of(' ');
if (pos == std::string::npos) {
return str;
}
std::string::size_type pos2 = str.find_last_not_of(' ');
if (pos2 != std::string::npos) {
return str.substr(pos, pos2 - pos + );
}
return str.substr(pos);
} void split(const std::string& str, std::string sep, std::vector<std::string>& ret) {
if (str.empty()) {
return;
} std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = ; while (pos_begin != std::string::npos) {
comma_pos = str.find(sep, pos_begin);
if (comma_pos != std::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;
} std::string replace(const std::string& str, const std::string& src, const std::string& dest) {
std::string ret;
std::string::size_type pos_begin = ;
std::string::size_type pos = str.find(src);
while (pos != std::string::npos) {
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;
} } // namespace strtool
#include <vector> //std::vector

namespace strtool{

std::string trim(const std::string& str);

void split(const std::string& str, std::string sep, std::vector<std::string>& ret);

std::string replace(const std::string& str, const std::string& src, const std::string& dest);

} // namespace strtool
#include <iostream>
#include <vector>
#include <string> void split(const std::string& str, std::string sep, std::vector<std::string>& ret) {
if (str.empty()) {
return;
}
std::string tmp;
std::string::size_type pos_begin = str.find_first_not_of(sep);
std::string::size_type comma_pos = ; while (pos_begin != std::string::npos) {
comma_pos = str.find(sep, pos_begin);
if (comma_pos != std::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;
} int main(void) {
std::string line = "a\tb\tc\td";
std::string sep = "\t";
std::vector<std::string> tokens;
split(line, sep, tokens);
int len = tokens.size();
for (int i = ; i < len; i++){
std::cout << tokens[i] << std::endl;
}
}

c++ 字符串工具类的更多相关文章

  1. StringUtils 字符串工具类

    package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...

  2. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  3. * 类描写叙述:字符串工具类 类名称:String_U

    /****************************************** * 类描写叙述:字符串工具类 类名称:String_U * ************************** ...

  4. Jsoup请求http或https返回json字符串工具类

    Jsoup请求http或https返回json字符串工具类 所需要的jar包如下: jsoup-1.8.1.jar 依赖jar包如下: httpclient-4.5.4.jar; httpclient ...

  5. StringUtil字符串工具类

    package com.zjx.test03; /** * 字符串工具类 * @author * */ public class StringUtil { /** * 判断是否是空 * @param ...

  6. 产生UUID随机字符串工具类

    产生UUID随机字符串工具类 UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成的API.按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址. ...

  7. 自用java字符串工具类

    不断封装一些常用的字符串操作加到这个工具类里,不断积累: package com.netease.lede.qa.util; import java.text.ParseException; impo ...

  8. String字符串工具类

    字符串类(StringUtil.cs) using System; namespace Sam.OA.Common { /// <summary> /// 字符处理工具类 /// 作者:陈 ...

  9. StringUtils字符串工具类左侧补齐(leftPad)、右侧补齐(rightPad)、左右两侧补齐(center)工具方法

    这里使用的是 org.apache.commons.lang.StringUtils;下面是StringUtils工具类中字符串左侧补齐的方法,示例如下: //左侧补齐 第一个参数:原始字符串,第二个 ...

  10. 字符串工具类ToStringBuilder常用方法介绍

    一.简介与引入   1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...

随机推荐

  1. .net中判断该应用程序是否已经启动,防止重复启动,监控程序启动是否正常

    //获取配置文件中的需要监控项 private static string MonitorServe = ConfigurationSettings.AppSettings["Monitor ...

  2. 蒋金楠How ASP.NET MVC Works?[持续更新中…]

    一.ASP.NET + MVC IIS与ASP.NET管道 MVC.MVP以及Model2[上篇]  MVC.MVP以及Model2[下篇] ASP.NET MVC是如何运行的[1]: 建立在“伪”M ...

  3. Android之帧动画2

    创建自定义对话框: // 对话框构建器 Builder builder = new AlertDialog.Builder(this); // 创建出一个空的对话框 final AlertDialog ...

  4. mysql 打包表在phpmyadmin提示正在使用中..

    一,利用phpmyadmin修改表功能,REPAIR TABLE `你的表名` 或直接在数据库管理界面,选中表如下图 二,如果利用修改功能失败了我们还可以尝试在替换本地mysql数据库时,我们先停止m ...

  5. OC2_点语法(属性关键字)

    // // Dog.h // OC2_点语法(属性关键字) // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhang ...

  6. MyEclipse导入ant项目——Java编程思想

    北门煎饼东门串儿: <JAVA编程思想(Think in Java)>一书中提供了大量源代码,可是项目是用ant构建的.对于用惯了eclipse,netbeans等IDE的同学们可能有些手 ...

  7. 关键字 extern

    定义:extern可置于变量或者函数前,以表示变量或者函数的定义在别的文件中.编译器会到其他模块中寻找其定义. extern int f(); extern int i; extern关键字   作为 ...

  8. CVTE面试总结

    刚面完了CVTE的一面,感觉完败,现总结. 一:准备不充分 这个跟时间仓促也有关系.昨天下午收到通知,晚上九点多回来填写给定格式的简历,题目也比较多.由于时间较仓促,那些问题的答案没有过多斟酌.但糟糕 ...

  9. Linux 小记录

    <1>bzero 原型:extern void bzero(void *s, int n);用法:#include <string.h> 功能:置字节字符串s的前n个字节为零. ...

  10. Mysql创建函数时报错

    先去查询  show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_f ...