首页 > 编程语言 >c++ 字符串常用操作

c++ 字符串常用操作

时间:2022-09-21 12:56:14浏览次数:93  
标签:std trim 常用 string c++ static 字符串 inline find

查找搜索

一般使用 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

标签:std,trim,常用,string,c++,static,字符串,inline,find
From: https://www.cnblogs.com/ramlife/p/16715216.html

相关文章

  • qmlRegisterType 注册C++类型出现 module not fount
    使用 qmlRegisterSingletonType或 qmlRegisterType想QML注册C++类,按照使用文档上方法添加如下:qmlRegisterSingletonType<CProtoInfoModel>("LdpModel",1,0,"p......
  • 你应该知道的 Python F 字符串技巧
    Python你应该知道的PythonF字符串技巧停止打印(f”variable={variable}”)Photoby约尔格·安杰利on不飞溅早在2016年,Python3.6就引入了一种新的字符串格......
  • linux 常用命令(四)
    which:命令的功能是用于查找命令文件,能够快速搜索二进制程序所对应的位置。whereis:命令用来定位命令的二进制程序、源代码文件和man手册页等相关文件的路径。who:命令的功......
  • 排序方法(C++ 、递归方法)
    1#include<iostream>2#include<vector>3usingnamespacestd;45vector<int>sort(intn,vector<int>inputs,intp){6intmin=inputs[p],pos......
  • 字符串
    字符串:字符串#定义字符串变量name="helloworld!"name2='HELLOWORLD'name3='nameis{name}andiam{year}old'#首字母大写print(name.capitalize())#......
  • 数据库删除常用方法与区别分析 TRUNCATE和DELETE只删除数据,DROP则删除整个表(结构和数
    数据库删除常用方法与区别分析TRUNCATE和DELETE只删除数据,DROP则删除整个表(结构和数据)学习如何使用数据库是大多数软件编程开发程序员都应该熟练掌握的一个编程技......
  • WPF 从 RGB 字符串转纯色颜色画刷的方法
    本文告诉大家几个方法用来从RGB字符串转纯色的SolidColorBrush画刷在Windows下,约定的编程规范里,颜色的RGB的字符串表示方法是#[A]RGB的格式,一定是R红色,接着是......
  • C++ 参考网站汇总
    本篇总结学习C++时常用的几个网站,点击会跳转到相应网页。一、CPP基础知识参考链接1.C++参考手册(英文版):https://en.cppreference.com/2.C++参考手册(中文版):https:/......
  • C++ 左值引用与一级指针
    将左值引用用于一级指针时,有以下几种用法://方式一:引用一级指针,常规用法inta=5;int*pa=&a;int*&rpa=pa;//方式二:引用指向常量的一级指针,以下几种为等效表......
  • C++07_std::tuple、std::optional、std::variant、std::visit
    std::tuplestd::tuple常用容器:tuplestd::tuple<...>可以将多个不同类型的值打包成一个。尖括号里填各个元素的类型。之后可以用std::get<0>获取第0个元素,std::get<1>......