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 ...
随机推荐
- CSS—换行
关于文本溢出换行问题,先看下涉及到换行的相关属性,查看:http://www.w3school.com.cn 一.word-break 定义:word-break 属性规定自动换行的处理方法. 值 描 ...
- 从零单排Linux – 1 – 简单命令
从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...
- javascript应用:页面解析list和map封装后的json数据
开发web项目时,经常会使用到的页面脚本语言javascript,使用它能让页面展示上的效果更多彩. 今天就来说一下,从数据库中获取到数据后在页面上的展示方式: 1.数据库取出数据放入list< ...
- 关于java的static关键字
通常来说,当你创建类时,就是在描述那个类的对象的外观与行为.除非你用new创建那个类的对象,否则,你实际上并未获得任何东西.当你用new来创建对象时,数据存储空间才被分配,其方法才供外界调用. 但是有 ...
- AIDL实现Android IPC
1.AIDL文本解释 在软件工程中,接口定义语言(IDL)已经成为通用术语,是用来描述软件组件接口的特定语言.在Android中,该IDL被称为Android接口定义语言(AIDL),它是纯文本文件, ...
- kettle列转行
- Poj 2996 Help Me with the Game
1.Link: http://poj.org/problem?id=2996 2.Content: Help Me with the Game Time Limit: 1000MS Memory ...
- 从一个乘法来分析C语言
昨天碰到一个很奇怪的问题,首先来看这段代码: #include<stdio.h> int main(int argc,char *argv[]) { ; ; long long res1 ...
- jquery实现全选、全不选、反选操作
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- google map 点与点画线
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...