简体   繁体   English

字符串比较C ++

[英]String comparison C++

I want to check szDir in szSelectedDir . 我想在szDir中检查szSelectedDir I want the function to consider the below inputs are different. 我希望函数考虑以下输入内容。 My sample function is not handling the scenario, please let me know the good solutions. 我的示例函数未处理该场景,请让我知道好的解决方案。

string szSelectedDir ="C:\\Windows1\Test";
string szDir="C:\\Windows";

void IsWindowsDirectory(const string szSpecialPath, const string szSelectedPath)
{

    if(szSelectedPath.compare(szSpecialPath) == 0)
    {
        printf("Both paths are same ");

    }
    else if(szSelectedPath.find(szSpecialPath) != string::npos)
    {
        printf("Both paths are same ");
    }
    else
    {
        printf("Both paths are different ");

    }

}

To check szDir in szSelectedDir . szDir中检查szSelectedDir

if (szSelectedDir.find(szDir) != string::npos)
{
    // You found szDir in szSelectedDir
}

As someone already said, comparing strings in C++ is pretty straightforward... 就像已经说过的那样,比较C ++中的字符串非常简单...

if (szSelectedPath == szSpecialPath)

I suggest you to pass strings by reference (strings not null, amongst other things). 我建议您通过引用传递字符串(字符串不为null等等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM