如何从字符串位置构造CSTRING/STD :: String

How to construct a CString/std::string from a string position

本文关键字:CSTRING STD String 字符串 位置      更新时间:2023-10-16

给定以下情况:

for( std::string line; getline( input, line ); )
{
        CString strFind = line.c_str();
        int n = strFind.ReverseFind( '' );
        CString s = CString( strFind,n );
        cout << s << endl;
      // m_Path.push_back( line.c_str() );  
}

它正在读取.ini配置,因此,我有一条线:

c: downloads insanity program 7。world.exe

此行添加到vector<CString>中。

我的问题是 int n = strFind.ReverseFind( '' );找到了第一个从字符串末端到开始的第一个 search的字符串pos,在构造像这样的 CString s = CString( strFind,n );的cstring之后,我在字符串上构造了第一个n个字符,因此 s是相等的 C:DownloadsInsanityProgram但是我想要的是将7 .World.exe复制到CCSTRING s,而不是相反,我该如何使用CStringstd::string

您是否仅针对ReverseFind功能将std::string转换为CString?如果是这样,您可以改用std::basic_string::find_last_of

#include <iostream>
#include <string>
int main()
{
  std::string s(R"(C:DownloadsInsanityProgram7. World.exe)");
  auto pos = s.find_last_of( '' ) + 1; //advance to one beyond the backslash
  std::string filename( s, pos );
  std::cout << filename << std::endl;
}

怎么样:

CString s = strFind.Mid(n+1);

或:

std::string s = line.substr(n+1);