ramlife

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

查找搜索

一般使用 find, rfind 即可,如果找不到,返回 std::npos.

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;

    string s1("Source Code");
    int n;
    if ((n = s1.find('u')) != string::npos) //查找 u 出现的位置
        cout << "1) " << n << "," << s1.substr(n) << endl;
    //输出 l)2,urce Code
    if ((n = s1.find("Source", 3)) == string::npos)
        //从下标3开始查找"Source",找不到
        cout << "2) " << "Not Found" << endl;  //输出 2) Not Found

参考:
学习笔记 c++ (在String查找子串和字符 )
https://blog.csdn.net/qq_42145185/article/details/101025298

c++ string类字符串查找
https://blog.csdn.net/qq_45797026/article/details/108359461

C++ string类中的字符串查找
https://blog.csdn.net/cywosp/article/details/7280466

C++字符串查找函数详解
http://c.biancheng.net/view/1453.html

字符串查找函数,C语言字符串查找函数详解
http://c.biancheng.net/view/340.html

删除特定字符

先用 remove 把字符移动,再用 erase 删除这些字符。

str.erase(std::remove(str.begin(), str.end(), 'a'), str.end());

参考:
C++从string中删除所有的某个特定字符
https://www.cnblogs.com/7z7chn/p/6341453.html

C++ remove、remove_copy、remove_if和remove_copy_if函数使用详解
http://c.biancheng.net/view/617.html

分割

使用 find 和 substr 配合

size_type find( const basic_string& str, size_type pos = 0 ) const;
basic_string substr( size_type pos = 0, size_type count = npos ) const;

// 使用字符串分割
void Stringsplit(const string& str, const string& splits, vector<string>& res)
{
	if (str == "")		return;
	//在字符串末尾也加入分隔符,方便截取最后一段
	string strs = str + splits;
	size_t pos = strs.find(splits);
	int step = splits.size();

	// 若找不到内容则字符串搜索函数返回 npos
	while (pos != strs.npos)
	{
		string temp = strs.substr(0, pos);
		res.push_back(temp);
		//去掉已分割的字符串,在剩下的字符串中进行分割
		strs = strs.substr(pos + step, strs.size());
		pos = strs.find(splits);
	}
}

参考:
C++中string如何实现字符串分割函数split()
https://blog.csdn.net/weixin_43919932/article/details/111304250

C++string字符串split的6种方法
https://zhuanlan.zhihu.com/p/426939341

C++ find()函数用法详解(超级详细)
http://c.biancheng.net/view/7489.html

trim

c++11 支持

#include <algorithm> 
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
        return !std::isspace(ch);
    }));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
        return !std::isspace(ch);
    }).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}
#include <algorithm> 
#include <functional> 
#include <cctype>
#include <locale>

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(),
            std::not1(std::ptr_fun<int, int>(std::isspace))));
}

// trim from end (in place)
static inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(),
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}

// trim from both ends (in place)
static inline void trim(std::string &s) {
    ltrim(s);
    rtrim(s);
}

// trim from start (copying)
static inline std::string ltrim_copy(std::string s) {
    ltrim(s);
    return s;
}

// trim from end (copying)
static inline std::string rtrim_copy(std::string s) {
    rtrim(s);
    return s;
}

// trim from both ends (copying)
static inline std::string trim_copy(std::string s) {
    trim(s);
    return s;
}

参考:
c++ 中关于string trim问题
https://www.cryogeny.cn/1310.html

C++实现trim()函数
https://blog.csdn.net/hugoo_hu/article/details/7947837

C语言写的trim()函数
https://www.cnblogs.com/liushui-sky/p/5584763.html

C++ Best way to trim std::string
https://cppsecrets.com/users/41129711010797106994610011511264103109971051084699111109/C00-Best-way-to-trim-stdstring.php

posted on 2022-09-21 12:46  ramlife  阅读(116)  评论(0编辑  收藏  举报