PowerShell: String Methods

By Xah Lee. Date: . Last updated: .

String are .NET objects, and have methods. String methods are more efficient than String Operators, and String Operators are more efficient than PowerShell Cmdlets. [see PowerShell: What is Cmdlet]

String Methods

# list string methods
"a" | Get-Member -MemberType Method
Clone
  • System.Object Clone()
  • System.Object ICloneable.Clone()
CompareTo
  • int CompareTo(System.Object value)
  • int CompareTo(string strB)
  • int IComparable.CompareTo(System.Object obj)
  • int IComparable[string].CompareTo(string other)
Contains
  • bool Contains(string value)
  • bool Contains(string value, System.StringComparison comparisonType)
  • bool Contains(char value)
  • bool Contains(char value, System.StringComparison comparisonType)
"abc".contains("bc")
CopyTo
  • void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EndsWith
  • bool EndsWith(string value)
  • bool EndsWith(string value, System.StringComparison comparisonType)
  • bool EndsWith(string value, bool ignoreCase, cultureinfo culture)
  • bool EndsWith(char value)
EnumerateRunes
  • System.Text.StringRuneEnumerator EnumerateRunes()
Equals
  • bool Equals(System.Object obj)
  • bool Equals(string value)
  • bool Equals(string value, System.StringComparison comparisonType)
  • bool IEquatable[string].Equals(string other)
GetEnumerator
  • System.CharEnumerator GetEnumerator()
  • System.Collections.IEnumerator IEnumerable.GetEnumerator()
  • System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator()
GetHashCode
  • int GetHashCode()
  • int GetHashCode(System.StringComparison comparisonType)
GetPinnableReference

?

GetType
  • type GetType()
GetTypeCode
  • System.TypeCode GetTypeCode()
  • System.TypeCode IConvertible.GetTypeCode()
IndexOf
  • int IndexOf(char value)
  • int IndexOf(char value, int startIndex)
  • int IndexOf(char value, System.StringComparison comparisonType)
  • int IndexOf(char value, int startIndex, int count)
  • int IndexOf(string value)
  • int IndexOf(string value, int startIndex)
  • int IndexOf(string value, int startIndex, int count)
  • int IndexOf(string value, System.StringComparison comparisonType)
  • int IndexOf(string value, int startIndex, System.StringComparison comparisonType)
  • int IndexOf(string value, int startIndex, int count, System.StringComparison comparisonType)
IndexOfAny
  • int IndexOfAny(char[] anyOf)
  • int IndexOfAny(char[] anyOf, int startIndex)
  • int IndexOfAny(char[] anyOf, int startIndex, int count)
Insert
  • string Insert(int startIndex, string value)
"abc".Insert(1, "X")
# aXbc
IsNormalized
  • bool IsNormalized()
  • bool IsNormalized(System.Text.NormalizationForm normalizationForm)
LastIndexOf
  • int LastIndexOf(char value)
  • int LastIndexOf(char value, int startIndex)
  • int LastIndexOf(char value, int startIndex, int count)
  • int LastIndexOf(string value)
  • int LastIndexOf(string value, int startIndex)
  • int LastIndexOf(string value, int startIndex, int count)
  • int LastIndexOf(string value, System.StringComparison comparisonType)
  • int LastIndexOf(string value, int startIndex, System.StringComparison comparisonType)
  • int LastIndexOf(string value, int startIndex, int count, System.StringComparison comparisonType)
LastIndexOfAny
  • int LastIndexOfAny(char[] anyOf)
  • int LastIndexOfAny(char[] anyOf, int startIndex)
  • int LastIndexOfAny(char[] anyOf, int startIndex, int count)
Normalize
  • string Normalize()
  • string Normalize(System.Text.NormalizationForm normalizationForm)
PadLeft
  • string PadLeft(int totalWidth)
  • string PadLeft(int totalWidth, char paddingChar)
PadRight
  • string PadRight(int totalWidth)
  • string PadRight(int totalWidth, char paddingChar)
Remove
  • string Remove(int startIndex, int count)
  • string Remove(int startIndex)
Replace
  • string Replace(string oldValue, string newValue, bool ignoreCase, cultureinfo culture)
  • string Replace(string oldValue, string newValue, System.StringComparison comparisonType)
  • string Replace(char oldChar, char newChar)
  • string Replace(string oldValue, string newValue)
$x = "i love dogs"
$x.replace("dog", "cat")
$x
# i love dogs
Split
  • string[] Split(char separator, System.StringSplitOptions options)
  • string[] Split(char separator, int count, System.StringSplitOptions options)
  • string[] Split(Params char[] separator)
  • string[] Split(char[] separator, int count)
  • string[] Split(char[] separator, System.StringSplitOptions options)
  • string[] Split(char[] separator, int count, System.StringSplitOptions options)
  • string[] Split(string separator, System.StringSplitOptions options)
  • string[] Split(string separator, int count, System.StringSplitOptions options)
  • string[] Split(string[] separator, System.StringSplitOptions options)
  • string[] Split(string[] separator, int count, System.StringSplitOptions options)
StartsWith
  • bool StartsWith(string value)
  • bool StartsWith(string value, System.StringComparison comparisonType)
  • bool StartsWith(string value, bool ignoreCase, cultureinfo culture)
  • bool StartsWith(char value)
Substring
  • string Substring(int startIndex)
  • string Substring(int startIndex, int length)
"abcdefg".substring(1,2)
# bc
ToBoolean
  • bool IConvertible.ToBoolean(System.IFormatProvider provider)
ToByte
  • byte IConvertible.ToByte(System.IFormatProvider provider)
ToChar
  • char IConvertible.ToChar(System.IFormatProvider provider)
ToCharArray
  • char[] ToCharArray()
  • char[] ToCharArray(int startIndex, int length)
ToDateTime
  • datetime IConvertible.ToDateTime(System.IFormatProvider provider)
ToDecimal
  • decimal IConvertible.ToDecimal(System.IFormatProvider provider)
ToDouble
  • double IConvertible.ToDouble(System.IFormatProvider provider)
ToInt16
  • short IConvertible.ToInt16(System.IFormatProvider provider)
ToInt32
  • int IConvertible.ToInt32(System.IFormatProvider provider)
ToInt64
  • long IConvertible.ToInt64(System.IFormatProvider provider)
ToLower
  • string ToLower()
  • string ToLower(cultureinfo culture)
ToLowerInvariant
  • string ToLowerInvariant()
ToSByte
  • sbyte IConvertible.ToSByte(System.IFormatProvider provider)
ToSingle
  • float IConvertible.ToSingle(System.IFormatProvider provider)
ToString
  • string ToString()
  • string ToString(System.IFormatProvider provider)
  • string IConvertible.ToString(System.IFormatProvider provider)
ToType
  • System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)
ToUInt16
  • ushort IConvertible.ToUInt16(System.IFormatProvider provider)
ToUInt32
  • uint IConvertible.ToUInt32(System.IFormatProvider provider)
ToUInt64
  • ulong IConvertible.ToUInt64(System.IFormatProvider provider)
ToUpper
  • string ToUpper()
  • string ToUpper(cultureinfo culture)
"hi".ToUpper()
ToUpperInvariant
  • string ToUpperInvariant()
Trim
  • string Trim()
  • string Trim(char trimChar)
  • string Trim(Params char[] trimChars)
TrimEnd
  • string TrimEnd()
  • string TrimEnd(char trimChar)
  • string TrimEnd(Params char[] trimChars)
TrimStart
  • string TrimStart()
  • string TrimStart(char trimChar)
  • string TrimStart(Params char[] trimChars)

PowerShell String


PowerShell in Depth

Path

Pipe

Comment

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples

Misc