Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

為 STL string 補上 trim、split、replace 與大小寫轉換

用過 .NET 的人大概都會懷念 String 那一整套好用的方法;相比之下 C++ 的 std::string 顯得樸素。下面整理一個 StringHelper 工具類,把最常需要的五個字串操作補齊。這裡以「好用」為優先,暫不追求極致性能。

#pragma once
#include <string>
#include <vector>
#include <algorithm>

class StringHelper
{
public:
    // 去除前後空白
    static std::string& trim(std::string& str)
    {
        if (str.empty()) return str;

        str.erase(0, str.find_first_not_of(" "));
        str.erase(str.find_last_not_of(" ") + 1);
        return str;
    }

    // 以 delim 中的任一字元為分隔符切開
    static std::vector<std::string> split(std::string& s, std::string& delim)
    {
        std::vector<std::string> ret;
        size_t last = 0;
        size_t index = s.find_first_of(delim, last);
        while (index != std::string::npos)
        {
            ret.push_back(s.substr(last, index - last));
            last = index + 1;
            index = s.find_first_of(delim, last);
        }
        if (index - last > 0)
        {
            ret.push_back(s.substr(last, index - last));
        }
        return ret;
    }

    // 全量替換
    static std::string replace(const std::string& str, const std::string& src, const std::string& dest)
    {
        std::string ret;
        std::string::size_type pos_begin = 0;
        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 + src.length();
            pos = str.find(src, pos_begin);
        }
        if (pos_begin < str.length())
        {
            ret.append(str.begin() + pos_begin, str.end());
        }
        return ret;
    }

    // 轉小寫
    static std::string tolower(const std::string str)
    {
        std::string data = str;
        std::transform(data.begin(), data.end(), data.begin(), ::tolower);
        return data;
    }

    // 轉大寫
    static std::string toupper(const std::string str)
    {
        std::string data = str;
        std::transform(data.begin(), data.end(), data.begin(), ::toupper);
        return data;
    }
};

測試程式

int _tmain(int argc, _TCHAR* argv[])
{
    std::string strTrim = "  hello world  ";
    printf("%s\n", StringHelper::trim(strTrim).c_str());

    std::string strSplit = "F:\\GreenSoft\\Adobe Photoshop CS5 Extended\\Photoshop\\Presets\\Actions";
    std::vector<std::string> strSet = StringHelper::split(strSplit, std::string("\\"));
    for (int i = 0; i < strSet.size(); i++)
    {
        printf("%d %s\n", i, strSet[i].c_str());
    }

    std::string strReplace = StringHelper::replace(strSplit, "Photoshop", "ps");
    printf("%s\n", strReplace.c_str());

    std::string strUpperLowerCase = "Hello World!";
    printf("%s\n", StringHelper::tolower(strUpperLowerCase).c_str());
    printf("%s\n", StringHelper::toupper(strUpperLowerCase).c_str());

    getchar();
    return 0;
}

使用上的注意點

trim 只處理半形空白,若要一併清掉定位標籤與換行,把 find_first_not_of 的字元集擴充為 " \t\r\n" 即可。另外 split 對「連續分隔符」會產生空字串項目,若不需要,記得在 push_back 前加一個空判斷;結尾的分隔符也可能被吃掉,這是 index - last > 0 判斷帶來的行為,依需求微調即可。

Copyright © Linux教程網 All Rights Reserved