定义字符串CString str="F:\\program\\test\\Debug\\test.exe",现在想去除"Debug\\test.exe",而保留获取前面的字符串。不能用substring方法,由于str是动态获取的,"Debug\\test.exe"之前的字符串会有变化。我尝试用减法运算,可是不行,请问有什么好的方法?

解决方案 »

  1.   

    SpanExcluding怎么样
    前面是动态的, "Debug\\test.exe"是不变的吧
      

  2.   

    CString strText= _T("F:\\program\\test\\Debug\\test.exe");
    int flag = -1;
    int nCount = 0;
    while(-1 != (flag = strText.ReverseFind(_T('\\'))))
    {
    if(2 == nCount++)
    break;
    strText = strText.Left(flag);
    }
    AfxMessageBox(strText);
      

  3.   

    只要"Debug\\test.exe"是固定的就可以啊CString str="F:\\program\\test\\Debug\\test.exe";
    CString szExe = "Debug\\test.exe";
    CString szResult = str.Left ( str.GetLength() - szExe.GetLength());
      

  4.   

    似乎不行,SpanExcluding检查的单个字符, "Debug\\test.exe"和前面有重复就不行了。
      

  5.   

    给你一个API 的TCHAR  str[] = _T("F:\\program\\test\\Debug\\test.exe");
    LPTSTR  p = StrStrI(str,_T("Debug\\Test.exe"));
    if (p) *p = 0x00;
    _tprintf(_T("%s"),str);