**string类提取字符串的问题***
我有一个串string str= "D:\C++ homework\AtivexTest\AtivexTestCtl.bmp ";
如何能快速的将这个文件名和路径提取出来呢?string类有没有好的方法?
像上面就是要提取出“D:\C++ homework\AtivexTest”
和“AtivexTestCtl.bmp "这两个串,大虾指点指点
[解决办法]
string str = "D:\\C++ homework\\AtivexTest\\AtivexTestCtl.bmp ";
string::size_type pos;
pos = str.rfind( "\\ ");
string str1(&str[pos] + 1, str.end());
string str2(str.begin(), &str[pos]);
cout < < str1 < < endl;
cout < < str2 < < endl;
[解决办法]
从str的最后一个字符读起,遇到 '\\ '或者 '/ ',就得到文件名与路径的相交处,设置为 '\0 ',就可以得到文件名,与路径名了
string str= "D:\\Data\\jet\\jack.bmp ";
//string str= "D:/Data/jet/jack.bmp ";/
int i=str.size();
for(; i> =0; i--)
{
if(str[i]== '/ ')
{
str[i]= '\0 ';
break;
}
else if(str[i]== '\\ ')
{
str[i]= '\0 ';
break;
}
}
cout < <str < < '\t ' < <str.c_str()+i+1 < <endl;
[解决办法]
串处理不是C++的强项,这种事情java和其他脚本语言做起来最舒服
[解决办法]
find_first_not_of
Syntax:
#include <string>
size_type find_first_not_of( const string& str, size_type index = 0 );
size_type find_first_not_of( const char* str, size_type index = 0 );
size_type find_first_not_of( const char* str, size_type index, size_type num );
size_type find_first_not_of( char ch, size_type index = 0 );
The find_first_not_of() function either:
returns the index of the first character within the current string that does not match any character in str, beginning the search at index, string::npos if nothing is found,
returns the index of the first character within the current string that does not match any character in str, beginning the search at index and searching at most num characters, string::npos if nothing is found,
or returns the index of the first occurrence of a character that does not match ch in the current string, starting the search at index, string::npos if nothing is found.
[解决办法]
注意路径中的 \ 需要使用转义字符