针对 std::string 的 trim 和 replace 方法

本文提供了一种在C++中实现字符串trim和replace操作的方法,通过自定义函数trim和replace来去除字符串首尾空白并替换指定子串,适用于std::string等类型。
部署运行你感兴趣的模型镜像
std::string不支持trim方法,同时原生的replace方法在使用上和其它语言也有些许差别,这里提供了两个替代方法,个人感觉更加好用。std::wstring等类似。

static int(__cdecl is_space)(int const c)
{
	return (c >= -127 && c <= 127 && isspace(c) != 0);
}

//-------------------------------------------------------------------------

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

//-------------------------------------------------------------------------

static void replace(std::string &s, 
					const char *p1, int l1,
					const char *p2, int l2)
{
	std::string::size_type pos = 0;
	while ((pos = s.find(p1, pos)) != std::string::npos) {
		s.replace(pos, l1, p2);
		pos = pos + l2;
	}
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

#include "../include/Log/Log.hpp" #include "../include/Log/Util.hpp" #include "../include/Log/LogFac.hpp" #include "Log/Cache.hpp" #include <iostream> #include <mutex> Log &Log::Instance() { static Log instance; return instance; } Log::Log() { logType = LogType::CONSOLE; logLevel = LogLevel::DEBUG; currentDay = Util::getCurrentDay(); fileNumber = 1; logFilePath = "app.log"; maxLogSize = "10MB"; } void Log::SetLogeType(const std::string& type) { if (type == "console") { logType = LogType::CONSOLE; } else if (type == "file") { logType = LogType::FILE; } else if(type == "remote") { logType = LogType::REMOTE; } else { logType = LogType::CONSOLE; std::cerr << "Unknown log type: " << type << ". Defaulting to CONSOLE." << std::endl; } } void Log::setLogType(const std::string& type) { SetLogeType(type); } void Log::SetLogLevel(const std::string& level) { if (level == "debug") { logLevel = LogLevel::DEBUG; } else if (level == "info") { logLevel = LogLevel::INFO; } else if (level == "warning") { logLevel = LogLevel::WARNING; } else if (level == "error") { logLevel = LogLevel::ERR; } else if (level == "fatal") { logLevel = LogLevel::FATAL; } else { logLevel = LogLevel::DEBUG; std::cerr << "Unknown log level: " << level << ". Defaulting to DEBUG." << std::endl; } } void Log::setLogLevel(const std::string& level) { SetLogLevel(level); } void Log::SetLogFilePath(const std::string &path) { logFilePath = path; } void Log::setLogFilePath(const std::string &path) { SetLogFilePath(path); } void Log::SetMaxLogSize(const std::string &size) { maxLogSize = size; } void Log::setMaxLogSize(const std::string &size) { SetMaxLogSize(size); } void Log::LogMessage(const std::string &message, LogLevel level, const std::string& threadId, const std::string& file, int line) { std::lock_guard<std::mutex> lock(mtx); if (level < logLevel) { return; } if (currentDay != Util::getCurrentDay()) { currentDay = Util::getCurrentDay(); fileNumber = 1; } std::string logMessage = Util::trim(format); logMessage = Util::replace(logMessage, "{time}", Util::getCurrentTime()); logMessage = Util::replace(logMessage, "{level}", Util::logLevelToString(level)); logMessage = Util::replace(logMessage, "{message}", message); logMessage = Util::replace(logMessage, "{thread_id}", threadId); logMessage = Util::replace(logMessage, "{file}", file); logMessage = Util::replace(logMessage, "{line}", std::to_string(line)); Cache::Instance().addLog(logMessage, logType, logFilePath, maxLogSize, fileNumber); } void Log::SetFormat(const std::string &fmt) { format = fmt; } void Log::setFormat(const std::string &fmt) { SetFormat(fmt); } void Log::logMessage(const std::string &message, LogLevel level, const std::string &threadId, const std::string &file, int line) { LogMessage(message, level, threadId, file, line); }
10-07
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值