【问题标题】:c++ small capital letter no differencec++小写字母没有区别
【发布时间】:2017-09-04 20:04:25
【问题描述】:

我有在其他字符串中查找字符串出现的方法。我的问题是如何让它不区分大小写?

int occurrences = 0;
string::size_type start = 0;

while ((start = base_string.find(to_find_occurrences_of, start)) != string::npos) {
    ++occurrences;
    start += to_find_occurrences_of.length(); 
}

【问题讨论】:

  • 什么语言?在某些语言中,一个小写/大写字母对应多种可能性,在某些情况下一次对应多个字符。

标签: c++


【解决方案1】:

为什么不在搜索前将两个字符串都设置为大写?

【讨论】:

    【解决方案2】:

    在比较之前将两个字符串标准化为大写或小写。

    这里解释了如何使用标准库的 tolower() 或 toupper() 函数和 transform() 函数:https://stackoverflow.com/a/313990/2355444

    【讨论】:

      【解决方案3】:

      检查这个答案 - 使用 std::search 和自定义谓词接缝对我来说是最好的方法

      https://stackoverflow.com/a/3152296/6910287

      【讨论】:

        【解决方案4】:

        使字符串中的所有字母大写/小。

        【讨论】:

          【解决方案5】:

          尝试在比较的两侧使用<cctype> (ctype.h) 库中的tolower() 或toupper() 函数,以便可以忽略任何差异。

          【讨论】:

            猜你喜欢
            • 2015-10-24
            • 2016-04-28
            • 2014-10-19
            • 1970-01-01
            • 1970-01-01
            • 2022-01-25
            • 1970-01-01
            • 1970-01-01
            • 2017-04-10
            相关资源
            最近更新 更多