String Object Methods in Windows PowerShell

Q

What methods are available on the string object in Windows PowerShell? I want to know which method to call to trim a string.

✍: FYIcenter.com

A

A String object supports the following methods in Windows PowerShell:

PS C:\fyicenter> "" | get-member -MemberType method

   TypeName: System.String

Name             MemberType Definition
----             ---------- ----------
Clone            Method     System.Object Clone(), System.Object IClone...
CompareTo        Method     int CompareTo(System.Object value), int Com...
Contains         Method     bool Contains(string value)
CopyTo           Method     void CopyTo(int sourceIndex, char[] destina...
EndsWith         Method     bool EndsWith(string value), bool EndsWith(...
Equals           Method     bool Equals(System.Object obj), bool Equals...
GetEnumerator    Method     System.CharEnumerator GetEnumerator(), Syst...
GetHashCode      Method     int GetHashCode()
GetType          Method     type GetType()
GetTypeCode      Method     System.TypeCode GetTypeCode(), System.TypeC...
IndexOf          Method     int IndexOf(char value), int IndexOf(char v...
IndexOfAny       Method     int IndexOfAny(char[] anyOf), int IndexOfAn...
Insert           Method     string Insert(int startIndex, string value)
IsNormalized     Method     bool IsNormalized(), bool IsNormalized(Syst...
LastIndexOf      Method     int LastIndexOf(char value), int LastIndexO...
LastIndexOfAny   Method     int LastIndexOfAny(char[] anyOf), int LastI...
Normalize        Method     string Normalize(), string Normalize(System...
PadLeft          Method     string PadLeft(int totalWidth), string PadL...
PadRight         Method     string PadRight(int totalWidth), string Pad...
Remove           Method     string Remove(int startIndex, int count), s...
Replace          Method     string Replace(char oldChar, char newChar),...
Split            Method     string[] Split(Params char[] separator), st...
StartsWith       Method     bool StartsWith(string value), bool StartsW...
Substring        Method     string Substring(int startIndex), string Su
ToBoolean        Method     bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte           Method     byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar           Method     char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray      Method     char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)
ToDateTime       Method     datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal        Method     decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble         Method     double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16          Method     int16 IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32          Method     int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64          Method     long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower          Method     string ToLower(), string ToLower(cultureinfo culture)
ToLowerInvariant Method     string ToLowerInvariant()
ToSByte          Method     sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle         Method     float IConvertible.ToSingle(System.IFormatProvider provider)
ToString         Method     string ToString(), string ToString(System.I...
ToType           Method     System.Object IConvertible.ToType(type conversionType, ...
ToUInt16         Method     uint16 IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32         Method     uint32 IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64         Method     uint64 IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper          Method     string ToUpper(), string ToUpper(cultureinfo culture)
ToUpperInvariant Method     string ToUpperInvariant()
Trim             Method     string Trim(Params char[] trimChars), string Trim()
TrimEnd          Method     string TrimEnd(Params char[] trimChars)
TrimStart        Method     string TrimStart(Params char[] trimChars)

Here is how you can trim a string using the trim() method:

PS C:\fyicenter> "  Hello world!   ".trim()
Hello world!

 

Work with Strings in Windows PowerShell

⇒⇒Windows PowerShell Tutorials

2016-10-19, 532👍, 0💬