c++ 字符串工具类
#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++ 字符串工具类的更多相关文章
- StringUtils 字符串工具类
package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import j ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- * 类描写叙述:字符串工具类 类名称:String_U
/****************************************** * 类描写叙述:字符串工具类 类名称:String_U * ************************** ...
- Jsoup请求http或https返回json字符串工具类
Jsoup请求http或https返回json字符串工具类 所需要的jar包如下: jsoup-1.8.1.jar 依赖jar包如下: httpclient-4.5.4.jar; httpclient ...
- StringUtil字符串工具类
package com.zjx.test03; /** * 字符串工具类 * @author * */ public class StringUtil { /** * 判断是否是空 * @param ...
- 产生UUID随机字符串工具类
产生UUID随机字符串工具类 UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成的API.按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址. ...
- 自用java字符串工具类
不断封装一些常用的字符串操作加到这个工具类里,不断积累: package com.netease.lede.qa.util; import java.text.ParseException; impo ...
- String字符串工具类
字符串类(StringUtil.cs) using System; namespace Sam.OA.Common { /// <summary> /// 字符处理工具类 /// 作者:陈 ...
- StringUtils字符串工具类左侧补齐(leftPad)、右侧补齐(rightPad)、左右两侧补齐(center)工具方法
这里使用的是 org.apache.commons.lang.StringUtils;下面是StringUtils工具类中字符串左侧补齐的方法,示例如下: //左侧补齐 第一个参数:原始字符串,第二个 ...
- 字符串工具类ToStringBuilder常用方法介绍
一.简介与引入 1.ToStringBuilder.HashCodeBuilder.EqualsBuilder.ToStringStyle.ReflectionToStringBuilder.Co ...
随机推荐
- Java Concurrency - 浅析 Phaser 的用法
One of the most complex and powerful functionalities offered by the Java concurrency API is the abil ...
- [转]IOS, xib和storyboard的混用
1. 从xib的viewcontroll中启动storyboard 或者 从一个storyboard切换到另一个storyboard: [objc]– (IBAction)openStoryboard ...
- JDBC之修改数据
文件分布图: 在MySQL中设置表格: Books: package com.caiduping.entity; public class Books { private int id; // 图书名 ...
- iOS开发 中的代理实现
iOS开发 中的代理实现 关于今天为什么要发这篇文字的原因:今天在和同事聊天的时候他跟我说项目中给他的block有时候不太能看的懂,让我尽量用代理写,好吧心累了,那就先从写个代理demo,防止以后他看 ...
- windows API 核心编程学习心得
一.错误处理 在内部,当windows函数检测到错误时,它会使用“线程本地存储区”的机制将相应的错误代码与“主调线程”关联到一起. winError.h 一般在C:\Program Files\Mic ...
- ASP.NET MVC 之控制器与视图之间的数据传递
今天,我们来谈谈控制器与视图之间的数据传递. 数据传递,指的是视图与控制器之间的交互,包括两个方向上的数据交互,一个是把控制器的数据传到视图中,在视图中如何显示数据,一个是把视图数据传递到控制器中, ...
- (转)使用CruiseControl+SVN+ANT实现持续集成之二
1. 环境搭建 1.1. 下载及目录介绍 从官方站点http://cruisecontrol.sourceforge.net/download.html下载一份最新的 CC 压缩包,最新的版本号为2. ...
- Windows 右键添加「cmd 打开」
1. 2. 3. 参考: 1.Windows右键添加"使用CMD打开" 2.WIN7.WIN8 右键在目录当前打开命令行Cmd窗口(图文)
- 《JSON必知必会》
每天上下班在地铁上很适合看这种书,入门级.难点不多.简约不失严谨. 自从全面转向ASP.NET MVC,现在基本上每天都和JSON打交道,效率.习惯.速度都要掌握. 这本书读起来很快,所以读完也蛮有成 ...
- Wildfly8 更改response header中的Server参数
项目经过局方安全检查需要屏蔽掉服务器中间件信息,查了一下午,网上看到的都是修改jboss7的,我们使用的wildfly8(jboss改名为wildfly),修改地方不一样,折磨了半天. jboss服务 ...