【问题标题】:finding last occurrence using boost string使用 boost 字符串查找最后一次出现
【发布时间】:2012-05-11 09:14:09
【问题描述】:

如何使用 boost 获得类似的功能

  int idx = md.filepath.lastIndexOf('/');                                 
      md.title = md.filepath.right(md.filepath.length() - idx -1);            
      md.title = md.title.left(md.title.length() - 4);      

上面的代码(基于QT)找到没有扩展名的文件名,md是一个对象,文件路径,标题是QString。我浏览了 boost 的字符串方法,例如 find_last,但它返回 iterator_range。我是新手,在此先感谢。

【问题讨论】:

    标签: c++ string boost


    【解决方案1】:

    如果您只想解析文件名,那么最好使用boost::filesystem::path 类。特别是以下方法:

    path filename() const;

    path stem() const; 和

    path extension() const;

    该类提供与std::string 和std::wstring 之间的转换。

    【讨论】:

    • 感谢您的回复。我正在寻找一种方法来获取没有扩展名的文件名。例如文件路径:/home/user/file.ext。我只想要'file'而不是'file.ext'
    • 这就是stem 函数为您提供的。
    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    • 2012-03-23
    • 2016-01-08
    • 1970-01-01
    相关资源
    最近更新 更多