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 ...
随机推荐
- Jersey(1.19.1) - JSON Support
Jersey JSON support comes as a set of JAX-RS MessageBodyReader<T> and MessageBodyWriter<T&g ...
- Linux 命令 - at: 在指定的时间执行任务
在指定的时间执行任务. 命令格式 at [-V] [-q queue] [-f file] [-mldbv] TIMEat [-V] [-q queue] [-f file] [-mldbv] -t ...
- Django学习--9 Admin
1.vim settings.py 打开 'django.contrib.admin' vim urls.py 打开 from django.contrib import admin (注意 ...
- 编译android5.0源码的
java环境 Android 5.1 用到的jdk不再是Oracle 的 jdk ,而是开源的 openjdk,在ubuntu安装好后,使用如下命令安装jdk: $sudo apt-get insta ...
- 【oracle】oracle函数-数值函数
一.数值函数 1. mod(m,n) 求余函数 注意:若m或者n为null,则返回null.若n为0,则返回m的值 eg:
- iOS 网络 -- cocoaPods 安装和使用教程
Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage CocoaPods 是什么? 当你开发iOS应用时, ...
- JSON对象(自定义对象)
JSON对象(自定义对象) 1.什么是JSON对象 JSON对象是属性的无序集合,在内存中也表现为一段连续的内存地址(堆内存) 1)JSON对象是属性的集合 2)这个集合是没有任何顺序的 2.JSON ...
- java实现的一个maven多模块项目自动生成工具
平时在做spring mvc web新项目时,都需要自己去搭建spring mvc的项目框架,包括基本pom 依赖引入,基本配置文件(web.xml,spring-mvc.xml,数据库配置文件等等) ...
- HOWTO re
\w 字母数字字符 [a-z A-Z 0-9_] \W 非字母数组字符 [^a-z-A-Z 0-9_] \d 十进制数字 [0-9] \D 非数字字符 [^0-9] \s 空白字符 [\t\n\r\f ...
- mysql 报Row size too large 65535 原因与解决方法
报错信息:Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535 ...