Sir/Madam,
I am learning the simple string methods in PS (which are .NET’s, I am told).
So far, CompareTo, [string]::Compare, StartsWith, EndsWith, ToLower, ToUpper, Replace all work well – i.e., PS ISE recognizes them. But when I use Contains, I get an error message stating that this is not recognized as a valid cmdlet or function.
Indeed, when I type ‘Con …’ the cycling process does not pop up the usual anticipated list of valid names.
My configuration must be lacking something. I have PS 5.1.14409.1012 on Windows 7 (64-bit) with latest fixes.
Would be grateful for any advice, tips or references.
Thanks in advance!
System.String objects in PowerShell have a contains method. If you want to see all the methods of an object, pipe the object to get-member. For example:
$foo = “This creates a string object”
$foo | gm
So if I wanted to use the contains method with the new $foo object I would just do the following example
$foo.contains(“creates”)
Thank you very much Mike … it now works. But I must be missing something fundamental in my understanding of PS as it relates to using existing methods in .NET. I cannot understand why the first few times I was writing simple 1-line code such as:
$a = “example”
$d = $a.Contains(“ample”)
I was getting the error message. And then a few repeated commands later, the error message no longer appears.
I hope to figure this out in due time.
Sincere thanks for your help.
set-psreadlineoption -EditMode Emacs
$a = 'example'
$a.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$a. # and press tab to see methods
Length GetType PadLeft ToChar ToLowerInvariant ToUpperInvariant
Clone GetTypeCode PadRight ToCharArray ToSByte Trim
CompareTo IndexOf Remove ToDateTime ToSingle TrimEnd
Contains IndexOfAny Replace ToDecimal ToString TrimStart
CopyTo Insert Split ToDouble ToType Chars
EndsWith IsNormalized StartsWith ToInt16 ToUInt16
Equals LastIndexOf Substring ToInt32 ToUInt32
GetEnumerator LastIndexOfAny ToBoolean ToInt64 ToUInt64
GetHashCode Normalize ToByte ToLower ToUpper
$a.contains # run it without the parentheses to see the definition
OverloadDefinitions
-------------------
bool Contains(string value)
[string}:: # and press tab for static methods
Empty Concat Format IsNullOrEmpty new
Compare Copy Intern IsNullOrWhiteSpace ReferenceEquals
CompareOrdinal Equals IsInterned Join
[string]::format # run it without the parentheses to see the definition
OverloadDefinitions
-------------------
static string Format(string format, System.Object arg0)
static string Format(string format, System.Object arg0, System.Object arg1)
static string Format(string format, System.Object arg0, System.Object arg1, System.Object arg2)
static string Format(string format, Params System.Object[] args)
static string Format(System.IFormatProvider provider, string format, System.Object arg0)
static string Format(System.IFormatProvider provider, string format, System.Object arg0, System.Object arg1)
static string Format(System.IFormatProvider provider, string format, System.Object arg0, System.Object arg1,
System.Object arg2)
static string Format(System.IFormatProvider provider, string format, Params System.Object[] args)