不区分大小写 std::string.find()
我正在使用 std::string 的 find() 方法来测试一个字符串是否是另一个字符串的子字符串。现在我需要相同内容的不区分大小写的版本。对于字符串比较,我总是可以使用 stricmp()
但似乎没有 stristr()
。
我找到了各种答案,大多数人建议使用 Boost
这在我的情况下不是一个选项。此外,我需要支持 std::wstring
/wchar_t
。有什么想法吗?
I am using std::string
's find()
method to test if a string is a substring of another. Now I need case insensitive version of the same thing. For string comparison I can always turn to stricmp()
but there doesn't seem to be a stristr()
.
I have found various answers and most suggest using Boost
which is not an option in my case. Additionally, I need to support std::wstring
/wchar_t
. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以将
std::search
与自定义谓词。You could use
std::search
with a custom predicate.新的 C++11 风格:
std::search 的说明可以在 cplusplus.com 上找到。
The new C++11 style:
Explanation of the std::search can be found on cplusplus.com.
为什么不使用 Boost.StringAlgo:
why not use Boost.StringAlgo:
为什么不在调用 find() 之前将两个字符串都转换为小写呢?
tolower
注意:
Why not just convert both strings to lowercase before you call
find()
?tolower
Notice:
由于您正在进行子字符串搜索(std::string)而不是元素(字符)搜索,因此不幸的是,据我所知,标准库中没有可以立即访问的现有解决方案来执行此操作。
不过,这很容易做到:只需将两个字符串都转换为大写(或都转换为小写 - 我在本例中选择大写)。
这不是一个快速的解决方案(接近悲观领域),但这是我所知道的唯一一个临时解决方案。如果您担心效率,那么实现自己的不区分大小写的子字符串查找器也不难。
语言环境中的 tolower/toupper 也适用于宽字符串,因此上面的解决方案应该同样适用(简单地将 std::string 更改为 std::wstring)。
[编辑] 正如所指出的,另一种方法是通过指定您自己的字符特征来从 basic_string 调整您自己的不区分大小写的字符串类型。如果您可以接受给定字符串类型的所有字符串搜索、比较等不区分大小写,则此方法有效。
Since you're doing substring searches (std::string) and not element (character) searches, there's unfortunately no existing solution I'm aware of that's immediately accessible in the standard library to do this.
Nevertheless, it's easy enough to do: simply convert both strings to upper case (or both to lower case - I chose upper in this example).
This is not a fast solution (bordering into pessimization territory) but it's the only one I know of off-hand. It's also not that hard to implement your own case-insensitive substring finder if you are worried about efficiency.
tolower/toupper in locale will work on wide-strings as well, so the solution above should be just as applicable (simple change std::string to std::wstring).
[Edit] An alternative, as pointed out, is to adapt your own case-insensitive string type from basic_string by specifying your own character traits. This works if you can accept all string searches, comparisons, etc. to be case-insensitive for a given string type.
如果您想根据 Unicode 和区域设置规则进行“真实”比较,请使用 ICU 的
Collator
类。If you want “real” comparison according to Unicode and locale rules, use ICU’s
Collator
class.提供 Boost 版本也是有意义的:这将修改原始字符串。
或使用完美的 boost xpression 库
在此示例中,您应该请注意,您的搜索词没有任何正则表达式特殊字符。
Also make sense to provide Boost version: This will modify original strings.
or using perfect boost xpression library
In this example you should pay attention that your search word don't have any regex special characters.
有点脏,但是很短而且很漂亮。快速地。
A little bit dirty, but short & fast.
我喜欢 Kiril V. Lyadvinsky 和 抄送。但我的问题比不区分大小写更具体一些;我需要一个支持 Unicode 的惰性命令行参数解析器,它可以在处理字母数字字符串搜索时消除误报/否定,这些字符串搜索可能在用于格式化我正在搜索的字母数字关键字的基本字符串中包含特殊字符,例如
Wolfjäger
不应匹配jäger
,但
应该匹配。它基本上只是 Kiril/CC 的答案,并对字母数字精确长度匹配进行了额外处理。
I love the answers from Kiril V. Lyadvinsky and CC. but my problem was a little more specific than just case-insensitivity; I needed a lazy Unicode-supported command-line argument parser that could eliminate false-positives/negatives when dealing with alphanumeric string searches that could have special characters in the base string used to format alphanum keywords I was searching against, e.g.,
Wolfjäger
shouldn't matchjäger
but<jäger>
should.It's basically just Kiril/CC's answer with extra handling for alphanumeric exact-length matches.
最有效的方法
简单快速。
The Most Efficient Way
Simple and Fast.
wxWidgets有非常丰富的字符串API
wxString
可以用它来完成(使用大小写转换方式)
wxWidgets has a very rich string API
wxString
it can be done with (using the case conversion way)