如何截取下面格式字符串中的部分字符串?

jdxwind 2008-10-29 09:37:48
CString temp="C:\Documents and Settings\user\桌面\sample.txt";
也就是个文件路径的字符串,现在只想得到"sample",即"."之前到最后一个"\"的部分,如何做呢?
有点思路,却很模糊,写不出来,请教。。。。
...全文
167 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdxwind 2008-10-29
  • 打赏
  • 举报
回复
感谢诸位,分少,抱歉啊
jdxwind 2008-10-29
  • 打赏
  • 举报
回复
感谢啊,学习了····
sanshao27 2008-10-29
  • 打赏
  • 举报
回复
用strstr这个函数查找最后一个‘\’,它的返回值就是最后一段sample.txt,在怎么取出'.'前面的名字,方法太多了,见楼上,呵呵
weichaogui 2008-10-29
  • 打赏
  • 举报
回复
呵呵,用pornographer所说的ReverseFind()最简单。

CString str="C:\\Documents and Settings\\user\\桌面\\sample.txt";
int nStart = str.ReverseFind('\\');
int nEnd = str.ReverseFind('.');
CString strTmp = str.Mid(nStart+1, nEnd-(nStart+1));
AfxMessageBox(strTmp);
weichaogui 2008-10-29
  • 打赏
  • 举报
回复
查找最后一个"\\"的位置,查找最后一个"."的位置,中间的字串就是要找的字串。
CString str = "C:\\Documents and Settings\\user\\桌面\\sample.txt";
int nStart = str.Find("\\", 0);
int nEnd = str.Find(".", 0);
int nStartTmp = nStart;
int nEndTmp = nEnd;

while (nStartTmp > 0 || nEndTmp > 0)
{
nStartTmp = str.Find("\\", nStart+1);
nEndTmp = str.Find(".", nEnd+1);
nStart = nStartTmp > 0 ? nStartTmp : nStart;
nEnd = nEndTmp > 0 ? nEndTmp : nEnd;
}

CString strTmp = str.Mid(nStart+1, nEnd-(nStart+1));
AfxMessageBox(strTmp);
Dan_M 2008-10-29
  • 打赏
  • 举报
回复
CString temp = _T("C:\\Documents and Settings\\user\\桌面\\sample.txt"); 
CString str;
int i = 1;
while (i>0)
{
i = temp.Find(_T('\\'));
temp = temp.Mid(i+1);
}
int j = temp.Find(_T('.'));
temp = temp.Mid(0,j);
cout << temp ;

"C:\\Documents and Settings\\user\\桌面\\sample.txt"里必须用'\\'不然没法识别

pornographer 2008-10-29
  • 打赏
  • 举报
回复
temp = temp.Mid(temp.ReverseFind('\\'),temp.ReverseFind('.')-1);
pornographer 2008-10-29
  • 打赏
  • 举报
回复
temp.Mid(temp.ReverseFind('\\'),temp.ReverseFind('.')-1);
iBug168 2008-10-29
  • 打赏
  • 举报
回复
CString temp="C:\Documents and Settings\user\桌面\sample.txt"; 

temp.substr(temp.ReverseFind('\\'), temp.ReverseFind('.'))


没有怎么用过CString,不知道有没有substr这个方法,但是意思应该差不多吧.

希望对LZ有帮助.
Dan_M 2008-10-29
  • 打赏
  • 举报
回复
每次取'\'后面的字串,当没有'\'时,再取'.'前面的字串
zqh886 2008-10-29
  • 打赏
  • 举报
回复
CString temp="C:\Documents and Settings\user\桌面\sample.txt";
int n=temp.Find("sample");
CString str=temp.Mid(n,6);//6为sample字符串中字符的个数,则str为sample
xy_dream 2008-10-29
  • 打赏
  • 举报
回复
查找到最后一个"\"的位置,然后把后面的一部分取出子串A,再在A中找到"."的位置i,然后再从A取出i前面的子串
sys0004 2008-10-29
  • 打赏
  • 举报
回复

TCHAR *pFile = _T("C:\Documents and Settings\user\桌面\sample.txt");
TCHAR szFileName[MAX_PATH] = { 0 };
TCHAR *p = _tcsrchr(pFile, _T('\\'));
if (p && ++p)
{
_tcscpy(szFileName, p);
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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