Joh*_*ohn 15 c++ string substring
在我的C++程序中,我有字符串
string s = "/usr/file.gz";
Run Code Online (Sandbox Code Playgroud)
在这里,如何使脚本检查.gz扩展(无论文件名是什么)并将其拆分为"/usr/file"?
Ali*_*gus 43
您可以使用erase删除符号:
str.erase(start_position_to_erase, number_of_symbols);
Run Code Online (Sandbox Code Playgroud)
你可以使用find找到起始位置:
start_position_to_erase = str.find("smth-to-delete");
Run Code Online (Sandbox Code Playgroud)
Com*_* 10 10
怎么样:
// Check if the last three characters match the ext.
const std::string ext(".gz");
if ( s != ext &&
s.size() > ext.size() &&
s.substr(s.size() - ext.size()) == ".gz" )
{
// if so then strip them off
s = s.substr(0, s.size() - ext.size());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57392 次 |
| 最近记录: |