Discussion:
How can the square bracket be found in a string?
(too old to reply)
Brillig
2007-01-25 13:34:00 UTC
Permalink
Dear Chums,

Because the string "[abc]" contains "[", I would like the following to
return "True":

PS> if ( "[abc]" -contains "[") {Write-Host True} else {Write-Host False}

I realsie that there is probably some protection meatacharacter, but
backtick and backslash don't seem to work.
--
Regards,
Brillig.
Sung M Kim
2007-01-25 14:16:01 UTC
Permalink
Dear Chums,

Because the string "[abc]" contains "[", I would like the following to
return "True":

PS> if ( "[abc]" -contains "[") {Write-Host True} else {Write-Host False}

I am not so clear on this but to be able to use a "-contains" operator,
left-hand side of the operator should be a collection type

So, you would need to convert "[abc]" into a collection of strings or
characters
that you want to check for a containment.

Preprending "ToCharArray()" to "[abc]" should work just fine in yoru case.

[^_^]PS[115]>if ("[abc]".ToCharArray() -contains "[") {Write-Host True} else
{Write-Host False}
True
[^_^]PS[116]>if ("[abc]".ToCharArray() -contains "x") {Write-Host True} else
{Write-Host False}
False
/\/\o\/\/ [MVP]
2007-01-25 14:26:03 UTC
Permalink
two other ways are :

PS H:\> if ( "[abc]" -match "\[") {Write-Host True} else {Write-Host False}
True

PS H:\> if ( "[abc]" -like '*`[*') {Write-Host True} else {Write-Host False}
True
PS H:\> if ( "[abc]" -like "*``[*") {Write-Host True} else {Write-Host False}
True

Note the different escaping in the -match (regex) and -like (wildcard)
also note the difference in escaping with "" and ''

Greetings /\/\o\/\/
Post by Brillig
Dear Chums,
Because the string "[abc]" contains "[", I would like the following to
PS> if ( "[abc]" -contains "[") {Write-Host True} else {Write-Host False}
I am not so clear on this but to be able to use a "-contains" operator,
left-hand side of the operator should be a collection type
So, you would need to convert "[abc]" into a collection of strings or
characters
that you want to check for a containment.
Preprending "ToCharArray()" to "[abc]" should work just fine in yoru case.
[^_^]PS[115]>if ("[abc]".ToCharArray() -contains "[") {Write-Host True} else
{Write-Host False}
True
[^_^]PS[116]>if ("[abc]".ToCharArray() -contains "x") {Write-Host True} else
{Write-Host False}
False
Bruce Payette [MSFT]
2007-01-25 17:12:17 UTC
Permalink
You can also use the .contains() method on strings :-)

PS (1) > $s = "[abc]"; $s.Contains('[')
True

- bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by /\/\o\/\/ [MVP]
PS H:\> if ( "[abc]" -match "\[") {Write-Host True} else {Write-Host False}
True
PS H:\> if ( "[abc]" -like '*`[*') {Write-Host True} else {Write-Host False}
True
PS H:\> if ( "[abc]" -like "*``[*") {Write-Host True} else {Write-Host False}
True
Note the different escaping in the -match (regex) and -like (wildcard)
also note the difference in escaping with "" and ''
Greetings /\/\o\/\/
Post by Brillig
Dear Chums,
Because the string "[abc]" contains "[", I would like the following to
PS> if ( "[abc]" -contains "[") {Write-Host True} else {Write-Host False}
I am not so clear on this but to be able to use a "-contains" operator,
left-hand side of the operator should be a collection type
So, you would need to convert "[abc]" into a collection of strings or
characters
that you want to check for a containment.
Preprending "ToCharArray()" to "[abc]" should work just fine in yoru case.
[^_^]PS[115]>if ("[abc]".ToCharArray() -contains "[") {Write-Host True} else
{Write-Host False}
True
[^_^]PS[116]>if ("[abc]".ToCharArray() -contains "x") {Write-Host True} else
{Write-Host False}
False
Loading...