【问题标题】:Compare two std::strings to see if they match c++ [closed]比较两个 std::strings 以查看它们是否匹配 c++ [关闭]
【发布时间】:2019-02-14 15:05:53
【问题描述】:

我正在尝试比较两个 std::strings 以查看该字符串是否在第二个字符串中。但是,当我使用“8eN”执行此操作时,我无法正常工作并且不会返回正确的颜色 - 相反,当我使用“8e”时它可以正常工作,所以我不确定这里发生了什么,任何帮助都会是非常感谢!

使用 Microsoft Visual c++ 6.0(旧版应用程序:()

这是我的代码

int CTcborder2::ColorOfFill()
{
    CString csGrade = m_border->csPuc; // this will be "8eN"

    std::string s((LPCTSTR)csGrade); //convert to std::string
    std::string t = "8eN"; // see if this string is in std::string s
    
    if(strstr(s.c_str(), t.c_str())) //only works with "8e" when I use "8eN" it doesn't return the correctly. 
        return COLOR;
    
}

【问题讨论】:

  • C++ 6.0?这甚至意味着什么?
  • 哼。我以为CString 有一个 find 方法;保存所有转换的东西。文档怎么说?
  • 如果问题是关于std::string,您可以通过将CString csGrade = m_border->csPuc; 替换为独立的东西来进一步简化您的示例——因此更容易重现。
  • 我会在 Visual Studio 6.0 上坚持使用 CString

标签: c++ mfc


【解决方案1】:

当你不需要的时候不要在 C++ 中使用 C 函数,只需使用 std::string::findstd::search by

int CTcborder2::ColorOfFill()
{
    CString csGrade = m_border->csPuc; // this will be "8eN"

    std::string s((LPCTSTR)csGrade); //convert to std::string
    std::string t = "8eN"; // see if this string is in std::string s

    if(s.find(t) != std::string::npos)
        return COLOR;

}

【讨论】:

  • 大声笑,当您尝试帮助质量有问题的问题时,您会得到什么
  • @Default 现在更好了吗?
猜你喜欢
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
相关资源
最近更新 更多