若有cstring str="d:\aa\aa.txt"
我想删除未尾的"\aa.txt"等七个字符,得到CString str2="d:\aa"
怎么做?

解决方案 »

  1.   

    CString str1="d:\aa\aa.txt";
    CString str2=str1.Delete(5, 7);
      

  2.   

    CString有一个find()的方法,找到标志字符的位置,用left()之类的方法就可以了
      

  3.   

    首先确定你要去掉字符的开始位置:
    int n = str.ReverseFind(_T('\\'));
    然后:
    str = str.Left(str.GetLength() - n );
      

  4.   

    CString Mid( int nFirst ) const;
    throw( CMemoryException );CString Mid( int nFirst, int nCount ) const;
    throw( CMemoryException );
      

  5.   

    我提的问题不是这个,我的意思是能确定最后若干位,如:
    f:\aa\aaa.txt
    我能确定aaa.txt,现在我想去掉f:\aa\aaa.txt未尾的\aaa.txt的八个字符,怎么做,前面的字符个数不能确定。
      

  6.   

    ReverseFind,look more on msdn
      

  7.   

    CString str="d:\aa\aa.txt"
    str.Delete(5,7);
    AfxMessageBox(str);
      

  8.   

    哦,前面的不确定啊,那先服从“ stavck(关未明)”然后再搞
      

  9.   

    CString str1,str2;
    int m,n;
    str1="f:\aa\aaa.txt";
    str2="\aaa.txt";
    m=str2.GetLength();
    n=str1.GetLength();
    str1.Delete(n-m,m);你愿意确定什么确定什么吧
      

  10.   

    呵呵,刚上CSDN来准备问同一个问题,就发现楼主的贴子了,
    楼主不会正好也在取得文件路径吧?
      

  11.   

    这样吧:
    str = str.Left(str.GetLength() - 7);