PowerShell: String Methods
String are dotnet objects, and have methods. String methods are more efficient than String Operators, and String Operators are more efficient than PowerShell Cmdlet.
List String Methods
# list string methods "a" | Get-Member -MemberType Method
Get Substring
Substring
-
string Substring(int startIndex)
string Substring(int startIndex, int length)
# substring, from 2 to end "abcd".substring(2).equals("cd") # from index 1 to 2 "abcd".substring(1, 2).equals("bc")
String Replace
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)
# Case-Sensitive replace "dog".replace("dog", "cat").equals("cat") # case insensitive replace "DOG".replace("dog", "cat", $true, $null).equals("cat")
Split String to Array
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)
$x = "a b c".split(" ")
Check Substring
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") -eq $true
StartsWith
-
bool StartsWith(string value)
bool StartsWith(string value, System.StringComparison comparisonType)
bool StartsWith(string value, bool ignoreCase, cultureinfo culture)
bool StartsWith(char value)
EndsWith
-
bool EndsWith(string value)
bool EndsWith(string value, System.StringComparison comparisonType)
bool EndsWith(string value, bool ignoreCase, cultureinfo culture)
bool EndsWith(char value)
Get Substring Index
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)
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)
String Insert/Remove
Insert
-
string Insert(int startIndex, string value)
"abc".Insert(1, "X") # aXbc
Remove
-
string Remove(int startIndex, int count)
string Remove(int startIndex)
# remove from index 2 to end "abcdef".remove(2).equals("ab") # remove from index 2, 3 chars "abcdef".remove(2,3).equals("abf")
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)
String Comparison
Equals
-
bool Equals(System.Object obj)
bool Equals(string value)
bool Equals(string value, System.StringComparison comparisonType)
bool IEquatable[string].Equals(string other)
"abc".equals("abc") # True # is Case-Sensitive "abc".equals("ABC") # False
CompareTo
-
int CompareTo(System.Object value)
int CompareTo(string strB)
int IComparable.CompareTo(System.Object obj)
int IComparable[string].CompareTo(string other)
String Format
PadLeft
-
string PadLeft(int totalWidth)
string PadLeft(int totalWidth, char paddingChar)
PadRight
-
string PadRight(int totalWidth)
string PadRight(int totalWidth, char paddingChar)
ToUpper
-
string ToUpper()
string ToUpper(cultureinfo culture)
"hi".ToUpper()
ToUpperInvariant
-
string ToUpperInvariant()
ToLower
-
string ToLower()
string ToLower(cultureinfo culture)
ToLowerInvariant
-
string ToLowerInvariant()
Clone
-
System.Object Clone()
System.Object ICloneable.Clone()
CopyTo
-
void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
EnumerateRunes
-
System.Text.StringRuneEnumerator EnumerateRunes()
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()
IsNormalized
-
bool IsNormalized()
bool IsNormalized(System.Text.NormalizationForm normalizationForm)
Normalize
-
string Normalize()
string Normalize(System.Text.NormalizationForm normalizationForm)
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)
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)
PowerShell String
- PowerShell: String
- PowerShell: Quote String
- PowerShell: QUOTATION MARK Quoted String, String Expansion
- PowerShell: Here-String
- PowerShell: Escape Characters
- PowerShell: String Operators
- PowerShell: Join String
- PowerShell: Split String
- PowerShell: Format String
- PowerShell: String Methods
- PowerShell: String Wildcards
- PowerShell: Regular Expression Operators
- PowerShell: Regular Expression Syntax