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 ...
随机推荐
- MyBatis(3.2.3) - Configuring MyBatis using XML, Properties
The properties configuration element can be used to externalize the configuration values into a prop ...
- android doc 本地文档加载慢的解决办法
从来都是FQ上谷歌官网查文档,但是有时没办法FQ,就得用sdk本地的doc文档了,由于文档内部的一些javascript,font等也需要访问Google来加载,导致了打开本地网页也巨慢无比,甚至转了 ...
- Android Device Orientation
最近在处理相机拍照的方向问题,在Android Device的Orientation问题上有了些疑问,就顺便写个Demo了解下Android Device Orientation究竟是怎么个判断. A ...
- zookeeper启动报错(数据目录权限不对)
zookeeper启动报错日志: 2016-11-16 11:19:43,880 [myid:3] - INFO [WorkerReceiver[myid=3]:FastLeaderElection@ ...
- hive创建表带中文注释报错解决方法
hive创建带有中文注释的表报错: FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask ...
- UIPickerView常见属性、常见方法(包括代理方法和数据源方法)的一些说明
一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...
- SecureCRT for Linux突破30天使用限制
当然还有一种方法,就是当你试用点i agree到时候,在~/.vandyke/Config 会生成一个文件SecureCRT_eval.lic,删除以后就可以恢复30天试用
- mysql 主从同步 Last_SQL_Error
参考文章: http://kerry.blog.51cto.com/172631/277414/ http://hancang2010.blog.163.com/blog/static/1824602 ...
- .NET架构师技能体系
.NET架构师应该掌握什么样的技术?其实这个问题很简单,去看看招聘.NET架构师的公司的职位要求就知道了.比如:http://www.cnblogs.com/guwei4037/p/5615471.h ...
- gulp 不是内部或者外部命令 或者 webpack 不是内部或者外部命令
gulp安装也正常,但是就是查看gulp -v的时候报错,原因:缺少系统变量PATH或者PATH变量错误 提示:这个系统变量PATH,直接追加就好(多个变量值用分号;隔开),不要删除已经有的系统变量P ...