From 061c574ccea5a87bef474b6548d0881787b4c149 Mon Sep 17 00:00:00 2001 From: Jie Date: Mon, 6 Jan 2025 14:51:33 +0800 Subject: [PATCH] add string replace function --- include/toolkit.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/include/toolkit.h b/include/toolkit.h index a624599..1009322 100644 --- a/include/toolkit.h +++ b/include/toolkit.h @@ -2,10 +2,11 @@ #define TOOLKIT_H #include +#include #include #include -#include #include +#include #include //use for to_chars @@ -44,6 +45,28 @@ namespace toolkit{ } return value; } + + template + requires std::is_same_v || std::is_same_v + std::string replace_string(const std::string& str, T d, T e){ + std::string result = str; + if(d == e){ + return result; + } + size_t len = 0; + while(true){ + auto pos = result.find_first_of(d); + if(pos == std::string::npos){ + return result; + } + if constexpr(std::is_same_v){ + len = std::strlen(d); + }else{ + len = d.length(); + } + result = result.replace(pos, len ,e); + } + } } #endif