C++实现文件后缀名提取,文件后缀名截取

使用标准的函数接口来,以最简单的方式来做:

#include <string>
#include <iostream>

using namespace std;

// 获取文件类型
string MainWindow::fileType(string extention)
{
    //return extention.substr(extention.find_last_of('.')+1,extention.length()-1);
    //or
    //return extention.substr(extention.find_last_of('.') + 1);
    //or
    return   extention.erase(0,extention.find_last_of('.')+1);
}

因为会改变原字符串的内容,所以传入的参数需要是拷贝复制,不能是引用和指针。

猜你喜欢

转载自blog.csdn.net/wfjdemmye/article/details/83153224