c++ 字符串工具类

#include <iostream>
#include <vector>
using namespace std;

namespace strtool
{
string trim(const string& str)
{
    string::size_type pos = str.find_first_not_of(‘ ‘);
    if (pos == string::npos)
    {
        return str;
    }
    string::size_type pos2 = str.find_last_not_of(‘ ‘);
    if (pos2 != string::npos)
    {
        return str.substr(pos, pos2 - pos + 1);
    }
    return str.substr(pos);
}

int split(const string& str, vector<string>& ret_, string sep = ",")
{
    if (str.empty())
    {
        return 0;
    }

    string tmp;
    string::size_type pos_begin = str.find_first_not_of(sep);
    string::size_type comma_pos = 0;

    while (pos_begin != string::npos)
    {
        comma_pos = str.find(sep, pos_begin);
        if (comma_pos != 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 0;
}

string replace(const string& str, const string& src, const string& dest)
{
    string ret;

    string::size_type pos_begin = 0;
    string::size_type pos       = str.find(src);
    while (pos != string::npos)
    {
        cout <<"replacexxx:" << pos_begin <<" " << pos <<"\n";
        ret.append(str.data() + pos_begin, pos - pos_begin);
        ret += dest;
        pos_begin = pos + 1;
        pos       = str.find(src, pos_begin);
    }
    if (pos_begin < str.length())
    {
        ret.append(str.begin() + pos_begin, str.end());
    }
    return ret;
}

}

int main(int argc, char* argv[])
{
    cout << strtool::trim(" nihao ") <<"\n";

    vector<string> vt;
    strtool::split(",o h,,,nice,,,,,,,", vt);
    for (size_t i = 0; i < vt.size(); ++ i)
    {
        cout <<"out:" << vt[i] <<"\n";
    }

    string ret = strtool::replace("xxAxxxAxxAxx", "A", "B");
    cout <<"replace:" << ret <<"\n";
    return 0;
}
时间: 2024-08-02 06:47:27

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

字符串工具类(指定字符串的长度和判断是否为空等方法)

package com.sec.util; /** * 字符串工具类 * @author Administrator * */public class StringUtil { /** * 过滤<,>,\n 字符串的方法 * @param input * @return */ public static String filterHTML(String input){ if(input == null || input.length() == 0){ return input; } input

Android工具类之字符串工具类,提供一些字符串相关的便捷方法

/** * 字符串工具类,提供一些字符串相关的便捷方法 */ public class StringUtil { private StringUtil() { throw new AssertionError(); } /** * is null or its length is 0 or it is made by space * <p/> * <pre> * isBlank(null) = true; * isBlank("") = true; * isBl

字符串工具类

package com.cmos.ngoc.util; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 字符串工具类 * */ public final class StringUtil { /** Private Constructor **/ private StringUtil() { } /** * 判断字符串是否非null && 非空字符 * * @param param * @retur

StringUtils 字符串工具类

package com.thinkgem.jeesite.common.utils; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; i

字符串工具类、数组工具类、集合工具类、转型操作工具类、编码与解码操作工具类

package hjp.smart4j.framework.util; import org.apache.commons.lang3.StringUtils; /** * 字符串工具类 */ public final class StringUtil { /** * 字符串分隔符 */ public static final String SEPARATOR=String.valueOf((char)29); /** * 判断字符串是否为空 */ public static boolean i

* 类描述:字符串工具类 类名称:String_U

/****************************************** * 类描述:字符串工具类 类名称:String_U * ******************************************/ public class String_U { private String_U() { } /** * 检查手机号是否合法 * * @param phoneNum * @return boolean true 为手机号合法,false为手机号不合法 */ publi

字符串工具类(判断是否为空,是否不为空,过滤掉集合中的空格元素)

import java.util.ArrayList; import java.util.List; /** * 字符串工具类 * @author gabodouer * */ public class StringUtil { /** * 判断是否是空 * @param str * @return */ public static boolean isEmpty(String str) { if (str == null || "".equals(str)) { return tru

Java 字符串工具类持续更新中非原创

1 import java.util.ArrayList; 2 import java.util.List; 3 4 /** 5 * 字符串相关的工具类 6 * 7 * @author Fsx 8 * 9 */ 10 public class StringUtil { 11 /** 12 * 判断一个字符串是否为空或等于空字符串 13 * 14 * @param s 15 * 字符串 16 * @return 是否为空或空字符串 17 */ 18 public static final bool

【原创】字符串工具类--找出单元字符串

package week01; import java.util.regex.Matcher; import java.util.regex.Pattern; /**** * * 字符串帮助类 * @author csharper * @since 2014.10.08 * */ public class StringHelper { /*** * 找出单位字符串大小 * @param str 源字符串 * @return 单位字符串大小 */ public static int findUni

【原创】字符串工具类--驼峰法与下划线法互转

在实际项目开发中,会碰到这样的问题,数据库表结构设计好了,可实体类还没相应地弄出来.实体类的属性命名方法一般是驼峰法,而数据库中的表字段命名方法用的是下划线法.如果表的字段非常多,我们根据设计好的数据库字段再手动敲写一遍驼峰法的属性,这有点费时了.如何迅速地把数据库中的表字段变成我们所需要的驼峰式的属性呢? 解决方法有二,一是通过文本编辑工具,如EditPlus,Notepad++等,利用它们携带的正则替换功能来迅速实现:二是通过自己编写工具类来实现.至于第一种方法操作技巧,不在这边赘述.第二种