C/C++字符串截取的问题

//假设我有一个字符串路径:
string path = "D:\Program Files\opencv\A1.jpg";
//或者是这样的路径:
string path = "D:/Program Files/opencv/B11.jpg";
简而言之,就是获取路径中的文件名字“A1.jpg”和“B11.jpg”,如何用C++编程实现

#include
#include

using namespace std;

int main()
{
string strPath(""D:\Program Files\opencv\A1.jpg""); // string中的\是转义字符,\就是代表\
string strFileName;
int nPos = strPath.find_last_of('\');
if(nPos > 0)
{
strFileName = strPath.substr(nPos + 1, strPath.length() - nPos - 1);
cout << strFileName << endl;
}
return 0;
}

另外一种情况类似:

反向截取喽,碰到/或\结束

#include
#include
using std::cout;
using std::endl;
using std::string;
int main(void){
string str1="hi,test,hello";
string str2="test";
//搜索子串。返回子串第一个字符的索引
cout << str1.find(str2)<<endl;
//假设不存在,返回内置常量string::npos,在一些编译器中通常为4294967295
cout << str1.find('k')<<endl;
//从指定索引開始搜索
cout <<str1.find('h',2)<<endl;
//从指定索引搜索指定字符串的前n个字符
cout <<str1.find("her",1,2)<<endl;

//在指定字符集合中搜索字符,返回其索引
cout <<str1.find_first_of("AaEeIiOoUu")<<endl;
     //从指定索引处開始在指定字符集合中搜索字符
cout <<str1.find_first_of("AaEeIiOoUu",2)<<endl;
     //从指定索引处開始在指定字符集合中搜索指定长度字符
cout <<str1.find_first_of("AaEeIiOoUu",2,2)<<endl;

//在指定字符集合中逆向搜索字符,返回字符最后索引,相同也具有上面另外两个重载方法
cout <<str1.find_last_of("AaEeIiOoUu")<<endl;

//查找字符串中第一个不在字符集合中的字符
cout <<str1.find_first_not_of("AaEeIiOoUu")<<endl;
//查找字符串中最后一个不在字符集合中的字符
cout <<str1.find_last_not_of("AaEeIiOoUu")<<endl;

//逆向搜索,也具有和find()一样的重载方法
cout <<str1.rfind('l')<<endl;

//截取子串
string str3=str1.substr(3,4);
cout <<str3<<endl;
return 0;

}

char fn[100];
char path[500] = "D:\Program Files\opencv\A1.jpg";
char *ptr = strrchr(path , '\');
sprintf(fn,"%s",ptr+1);
或者
char fn[100];
char path[500] = "D:/Program Files/opencv/B11.jpg";
char *ptr = strrchr(path , '/');
sprintf(fn,"%s",ptr+1);

使用正则表达式匹配.和最后一个/实现

如果是Windows下的话有现成的代码:

 #include <iostream>   
#include <string>
#include <Shlwapi.h>
#pragma comment (lib,"Shlwapi.lib")
using namespace std;

int main(void)
{
    string path = "D:\\Program Files\\opencv\\A1.jpg";
    string path2 = "D://Program Files//opencv//B11.jpg";
    string ret = PathFindFileName(path.c_str());
    string ret2 = PathFindFileName(path2.c_str());
    system("pause");
    return 0;
}

利用C++ 标准的库函数应该可以实现。

用强大的boost库吧

 #include <boost/algorithm/string.hpp>
#include <iostream>
#include <string>


int main() {

    std::string path1 = "D:\\Program Files\\opencv\\A1.jpg";
    std::string path2 = "D:/Program Files/opencv/B11.jpg";

    /* 找到最后一个\\ */
    auto it1 = boost::find_last(path1,"\\");

    /* 找到最后一个/ */
    auto it2 = boost::find_last(path2, "/");

    /* name1为A1.jpg;it1.end()指向'A'的位置 */
    std::string name1 = std::string(it1.end(), path1.end());

    /* name2为B11.jpg;it1.end()指向'B'的位置 */
    std::string name2 = std::string(it2.end(), path2.end());

    std::cout << name1 << std::endl;
    std::cout << name2 << std::endl;

    system("pause");
}

先获取斜杠位置,然后截取

找到最后一个斜杠位置i=str.lastIndxOf('/'),然后截取ret=str.subString(i);

从字符串末尾开始一位一位的判断是否为‘/’或‘\’,若是则截取其后面的字符串

反向截取喽,碰到/或\结束,很简单的

Separate string with specific tokens (使用指定的一个或多个标记划分字符串)

Simply separate (简单划分)

string: "abc;def ghi:jkl"
delim: ":; "
  • abc
  • def
  • ghi
  • jkl
char *src = "abc;def ghi:jkl";
char *delim = ":; "

sub_str = token(str, delim);
printf("%s\n", sub_str);

while((sub_str = token(NULL, delim)) != NULL)
{
    printf("%s\n", sub_str);
}

Hierarchically separe (层级划分)

string: "a/bbb///cc;xxx:yyy:"
delim: ":;"
sub-delim: "/"
  • a/bbb///cc

    • a
    • bbb
    • cc
  • xxx

    • xxx
  • yyy

    • yyy
char *str1, *str2;
char *delim, *subdelim;
char *token, *subtoken;
char *saveptr1, *saveptr2;

str1 = "a/bbb///cc;xxx:yyy:";
delim = ";:";
subdelim = "/";

while(1) 
{
    token = strtok_r(str1, delim, &saveptr1);
    if(token == NULL)
        break;
    printf("-> %s", token);

    while(1)
    {
        sub_token = strtok_r(str2, subdelim, &saveptr2);
        if(sub_token == NULL)
            break;
        printf("--> %s", sub_token);
        str2 = NULL;
    }
    str1 = NULL;
}

找到最后一个\ 或者 找到最后一个/ , 截取就行了哇。。。。。

有库函数直接用就可以获取路径和文件名,扩展名

从末尾反向寻找\或/,截取

string s(path.substr(pos + 1) );

先获取斜杠位置,然后截取啊

如果是Windows下的话有现成的代码:

#include

#include
#include
#pragma comment (lib,"Shlwapi.lib")
using namespace std;

int main(void)
{
string path = "D:\Program Files\opencv\A1.jpg";
string path2 = "D://Program Files//opencv//B11.jpg";
string ret = PathFindFileName(path.c_str());
string ret2 = PathFindFileName(path2.c_str());
system("pause");
return 0;
}

利用std::string特性从后面寻找/,就可以了,

const string path = "D:\Program Files\opencv\A1.jpg";
int pos = path.rfind('\');
int len = path.length();
string sub = path.substr(pos, len);