截取字符串?

quwei197874 2011-06-22 02:08:40
string sTest = "D:\Type\family.txt";

上面的字符串我想把family截取出来,应该怎么做?

...全文
133 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-06-22
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
string sTest="D:\\Type\\family.txt";
string fe=sTest.substr(sTest.rfind("\\")+1);
cout << fe << endl; //family.txt
string f=fe.substr(0,fe.rfind("."));
cout << f << endl; //family

char fname[_MAX_FNAME];
_splitpath(sTest.c_str(),NULL,NULL,fname,NULL);
printf("%s\n",fname); //family

return 0;
}

_splitpath, _wsplitpath
Break a path name into components.

void _splitpath( const char *path, char *drive, char *dir, char *fname, char *ext );

void _wsplitpath( const wchar_t *path, wchar_t *drive, wchar_t *dir, wchar_t *fname, wchar_t *ext );

Routine Required Header Compatibility
_splitpath <stdlib.h> Win 95, Win NT
_wsplitpath <stdlib.h> or <wchar.h> Win 95, Win NT


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version


Return Value

None

Parameters

path

Full path

drive

Optional drive letter, followed by a colon (:)

dir

Optional directory path, including trailing slash. Forward slashes ( / ), backslashes ( \ ), or both may be used.

fname

Base filename (no extension)

ext

Optional filename extension, including leading period (.)

Remarks

The _splitpath function breaks a path into its four components. _splitpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wsplitpath is a wide-character version of _splitpath; the arguments to _wsplitpath are wide-character strings. These functions behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tsplitpath _splitpath _splitpath _wsplitpath


Each argument is stored in a buffer; the manifest constants _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, and _MAX_EXT (defined in STDLIB.H) specify the maximum size necessary for each buffer. The other arguments point to buffers used to store the path elements. After a call to _splitpath is executed, these arguments contain empty strings for components not found in path. You can pass a NULL pointer to _splitpath for any component you don’t need.

Example

/* MAKEPATH.C */

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];

_makepath( path_buffer, "c", "\\sample\\crt\\", "makepath", "c" );
printf( "Path created with _makepath: %s\n\n", path_buffer );
_splitpath( path_buffer, drive, dir, fname, ext );
printf( "Path extracted with _splitpath:\n" );
printf( " Drive: %s\n", drive );
printf( " Dir: %s\n", dir );
printf( " Filename: %s\n", fname );
printf( " Ext: %s\n", ext );
}


Output

Path created with _makepath: c:\sample\crt\makepath.c

Path extracted with _splitpath:
Drive: c:
Dir: \sample\crt\
Filename: makepath
Ext: .c


File Handling Routines

See Also _fullpath, _getmbcp, _makepath, _setmbcp
ryfdizuo 2011-06-22
  • 打赏
  • 举报
回复
lz的字符串本身就有问题:string sTest="D:\Type\family.txt";
应该是两个\\
赵4老师 2011-06-22
  • 打赏
  • 举报
回复
#include <string>
#include <iostream>
using namespace std;
int main() {
string sTest="D:\\Type\\family.txt";
string fe=sTest.substr(sTest.rfind("\\")+1);
cout << fe << endl; //family.txt
string f=fe.substr(0,fe.rfind("."));
cout << f << endl; //family
return 0;
}
c_losed 2011-06-22
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 bdmh 的回复:]

C/C++ code

string sTest = "D:\\Type\\family.txt";
int a = sTest.find_last_of(".");
int b = sTest.find_last_of("\\");
string s = sTest.substr(b+1,a);
[/Quote]

string s = sTest.substr(b+1,a-b-1);//ms应该这样
就想叫yoko 2011-06-22
  • 打赏
  • 举报
回复
strtok
sscanf

string的一系列find
sutaizi 2011-06-22
  • 打赏
  • 举报
回复
p1+1
sutaizi 2011-06-22
  • 打赏
  • 举报
回复
用 find_last_of(x) 得到x的位置 也就是说 得到最后一个‘/’ 的位置p1和最后一个‘.’的位置p2 然后substr(p1,p2)就能得到文件名了。
bdmh 2011-06-22
  • 打赏
  • 举报
回复

string sTest = "D:\\Type\\family.txt";
int a = sTest.find_last_of(".");
int b = sTest.find_last_of("\\");
string s = sTest.substr(b+1,a);
qq120848369 2011-06-22
  • 打赏
  • 举报
回复
size_t pos=find(0,".");

然后从pos往前找一个'\',就行了.
ryfdizuo 2011-06-22
  • 打赏
  • 举报
回复
// string::find_last_of
#include <iostream>
#include <string>
using namespace std;

void SplitFilename (const string& str)
{
size_t found;
cout << "Splitting: " << str << endl;
found=str.find_last_of("/\\");
cout << " folder: " << str.substr(0,found) << endl;
cout << " file: " << str.substr(found+1) << endl;
}

int main ()
{
string str1 ("/usr/bin/man");
string str2 ("c:\\windows\\winhelp.exe");

SplitFilename (str1);
SplitFilename (str2);

return 0;
}

Splitting: /usr/bin/man
folder: /usr/bin
file: man
Splitting: c:\windows\winhelp.exe
folder: c:\windows
file: winhelp.exe
ryfdizuo 2011-06-22
  • 打赏
  • 举报
回复
strtok
或者string.find
quwei197874 2011-06-22
  • 打赏
  • 举报
回复
c++
sutaizi 2011-06-22
  • 打赏
  • 举报
回复
什么语言

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧