将字符串与Qt::MatchFlag进行比较

将字符串与Qt::MatchFlag进行比较,qt,Qt,Qt是否提供了确定两个字符串匹配的方法? 例如: bool res = isMatched( "Hello World", "World", Qt::MatchContains ); 要确定一个字符串是否包含另一个字符串,可以使用QString::contains: QString str = "Hello World"; bool res = str.contains("World", Qt:::CaseSensitive); // returns true 您还可以使用QStri

Qt是否提供了确定两个字符串匹配的方法? 例如:

 bool res = isMatched( "Hello World", "World", Qt::MatchContains );

要确定一个字符串是否包含另一个字符串,可以使用
QString::contains

QString str = "Hello World";
bool res = str.contains("World", Qt:::CaseSensitive);    // returns true
您还可以使用
QString::compare
来比较字符串。如果第一个字符串小于、等于或大于第二个字符串,则返回小于、等于或大于零的整数:

int x = QString::compare(str1, str2, Qt::CaseInsensitive);

没有任何方法可以使用
Qt::MatchFlags
比较字符串,因为
Qt::MatchFlags
仅用于特殊目的:在表中查找项


这些标志包含
Qt::MatchWrap
Qt::MatchRecursive
,它们只对表有意义,而不仅仅对2个字符串有意义。

我在最近的一个项目中需要它,并编写了一个函数,可以轻松地添加到任何程序中。以下是代码的链接: 假设未来不会发生任何变化……以下是代码本身:

/** determine if the pattern matches the string using Qt::MatchFlags
    \param str the string
    \param pattern the pattern to find
    \param flags any combination of the follow Qt flags
                - Qt::MatchFixedString
                - Qt::MatchContains
                - Qt::MatchStartsWith
                - Qt::MatchEndsWith
                - Qt::MatchRegExp (overrides all flags above)
                - Qt::MatchCaseSensitive
    \returns true if the pattern is found in the string

    requires:
    #include <QString>
    #include <QRegularExpression>
    #include <QRegularExpressionMatch>

    Thank you for visiting sirspot.com
    This code is not guaranteed to work.
    Use at your own risk.
*/
static bool QString_Matches(
    const QString& str,
    const QString& pattern,
    const Qt::MatchFlags& flags = (Qt::MatchCaseSensitive | Qt::MatchFixedString))
{
    if(flags.testFlag(Qt::MatchRegExp) == true)
    {
        QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption;
        if(flags.testFlag(Qt::MatchCaseSensitive) == false)
        {
            options = QRegularExpression::CaseInsensitiveOption;
        }
        QRegularExpression regex(pattern, options);
        return regex.match(str).hasMatch();
    }
    else
    {
        Qt::CaseSensitivity cs = Qt::CaseSensitive;
        if(flags.testFlag(Qt::MatchCaseSensitive) == false)
        {
            cs = Qt::CaseInsensitive;
        }
        if(flags.testFlag(Qt::MatchContains) == true)
        {
            return str.contains(pattern, cs);
        }
        else
        {
            if(flags.testFlag(Qt::MatchStartsWith) == true)
            {
                if(str.startsWith(pattern, cs) == true)
                {
                    return true;
                }
            }
            if(flags.testFlag(Qt::MatchEndsWith) == true)
            {
                if(str.endsWith(pattern, cs) == true)
                {
                    return true;
                }
            }
            if(flags.testFlag(Qt::MatchFixedString) == true)
            {
                return (str.compare(pattern, cs) == 0);
            }
        }
    }
    return false;
};
/**使用Qt::MatchFlags确定模式是否与字符串匹配
\param str字符串
\param pattern要查找的模式
\param标志以下Qt标志的任意组合
-Qt::MatchFixedString
-Qt::MatchContains
-Qt::MatchStartsWith
-Qt::MatchEndsWith
-Qt::MatchRegExp(覆盖上面的所有标志)
-Qt::匹配区分大小写
\如果在字符串中找到模式,则返回true
要求:
#包括
#包括
#包括
感谢您访问sirspot.com
此代码不能保证工作。
使用风险自负。
*/
静态布尔QString_匹配(
const QString&str,
常量字符串和模式,
常量Qt::MatchFlags&flags=(Qt::MatchCaseSensitive | Qt::MatchFixedString))
{
if(flags.testFlag(Qt::MatchRegExp)==true)
{
QRegularExpression::PatternOptions=QRegularExpression::NoPatternOption;
if(flags.testFlag(Qt::MatchCaseSensitive)==false)
{
options=QRegularExpression::CaseInsensitiveOption;
}
QRegularExpression正则表达式(模式、选项);
返回regex.match(str.hasMatch();
}
其他的
{
Qt::区分大小写cs=Qt::区分大小写;
if(flags.testFlag(Qt::MatchCaseSensitive)==false)
{
cs=Qt::不区分大小写;
}
if(flags.testFlag(Qt::MatchContains)==true)
{
返回str.contains(模式,cs);
}
其他的
{
if(flags.testFlag(Qt::MatchStartsWith)==true)
{
if(str.startsWith(pattern,cs)==true)
{
返回true;
}
}
if(flags.testFlag(Qt::MatchEndsWith)==true)
{
if(str.endsWith(pattern,cs)==true)
{
返回true;
}
}
if(flags.testFlag(Qt::MatchFixedString)==true)
{
返回(str.compare(pattern,cs)==0);
}
}
}
返回false;
};

快乐的字符串匹配

当然,我可以使用QString方法找出匹配的字符串。但我希望得到答案——有没有一种通用的方法来达到这个目的。如果没有这样的方法-为什么?你说的“普通方法”是什么意思?在
QString
中有比较和检查字符串是否存在的方法。也许最好进一步澄清。@YuriyVelichko如果内贾特的答案不是你想要的,也许你可以在问题中详细说明你的目的,并解释什么是常用方法?另外,你可以看一看类。Yuriy,答案是肯定的,Qt提供了确定两个字符串匹配的方法——如上所述。你能解释一下与其他(较短/较简单)答案相比,你的方法的优势吗?@m7913d有两个主要优势:1。它使用已建立的匹配标志2。它允许使用单个函数参数更改字符串匹配条件。3.无需复制多行代码即可使用不同的字符串匹配方法。例如,我有一个搜索框,希望用户能够在“以”“精确匹配”“开始”和“正则表达式”之间进行选择因此,通过编写这个通用字符串匹配函数,我可以简单地将一组不同的标志传递给搜索算法,从而改变搜索的执行方式。