【问题标题】:How to construct a CString/std::string from a string position如何从字符串位置构造 CString/std::string
【发布时间】:2012-10-21 19:20:57
【问题描述】:

鉴于以下情况:

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 配置,在这个 .ini 上我有一行:

C:\Downloads\Insanity\Program\7.世界.exe

这一行被添加到vector&lt;CString&gt;。

我的问题是int n = strFind.ReverseFind( '\\\' ); 找到第一个 \ 的字符串 pos 从字符串的末尾搜索到开头,在构造像这样的 CString CString s = CString( strFind,n ); 之后,我正在构造字符串上的前 n 个字符,所以s 等于 C:\Downloads\Insanity\Program 但我想要将 7 .World.exe 复制到 CString s 而不是其他方式,我该如何使用 CString 或 std::string 来做到这一点?

【问题讨论】:

    标签: c++ cstring stdstring


    【解决方案1】:

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

    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s(R"(C:\Downloads\Insanity\Program\7. 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;
    }
    

    【讨论】:

      【解决方案2】:

      怎么样:

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

      或:

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

      【讨论】:

        猜你喜欢
        • 2012-05-08
        • 2019-05-13
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 2011-06-06
        • 2017-08-07
        • 2016-10-23
        • 2019-01-25
        相关资源
        最近更新 更多