[英]Removing certain characters at the beginning of string
Here's the usecase.这是用例。
I have got a string with relative path to the folder.我有一个带有文件夹相对路径的字符串。 It's format may vary a little bit depending on where it came from (I am dealing with exported files from difference software).
它的格式可能会有所不同,具体取决于它的来源(我正在处理从不同软件导出的文件)。
For example: ./path/to/folder
, /path/to/folder
, path/to/folder
.例如:
./path/to/folder
、 /path/to/folder
、 path/to/folder
。
What I need to do is to delete all the characters '.'
我需要做的是删除所有字符
'.'
, '/'
from the beginning of the string. ,
'/'
从字符串的开头。 Of course I can just do this manually in a for loop, but I thought maybe there's some kind of stl function exactly for such use-cases.当然,我可以在 for 循环中手动执行此操作,但我认为可能有某种 stl 函数正好适用于此类用例。
I thought maybe there's some kind of stl function exactly for such use-cases
我想也许有某种 stl 函数正好适合这样的用例
#include <regex>
const std::string src("./path/to/folder");
static const std::regex re("^\\.?\\/?");
const std::string result = std::regex_replace(src, re, "");
If you need more efficiency than what <regex>
provides, do it manually.如果您需要比
<regex>
提供的效率更高的效率,请手动执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.