开发者

Selecting strings and case sensitivity

I am trying to filter for an object that has a Title field, and I want to ignore case. Is there a way to make sure case sensitivity if turned off?

| Where-Objec开发者_如何学Ct {$_.Title -like "myString"}


PowerShell is fundamentally case insensitive (e.g. "HEy" -like "hey" is True).

If you want to use the case sensitive version of like, use -clike.


By default case sensitivity is off:

PS> 'test','TEST','TeSt','notest' | ? { $_ -like 'test' }
test
TEST
TeSt

From documentation:

By default, all comparison operators are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a "c". For example, the case-sensitive version of "-eq" is "-ceq". To make the case-insensitivity explicit, precede the operator with an "i". For example, the explicitly case-insensitive version of "-eq" is "-ieq".

For more information run help about_comparison_operators


A more generic approach, I think, would be to use regular expressions, which is especially handy for people who has learned regular expressions from another language such as Perl. Example:

'test','TEST','TeSt','notest' | ? { $_ -match '(?-i)^test' }
test
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜