Windows PowerShell
Your code sample:
PS> "source target destination".ToLower().Split("target")
is equivalent to:
PS> "source target destination".ToLower().Split( @("t", "a", "r", "g", "e", "t") )
because PowerShell is invoking the string[] Split(Params char[] separator)
overload of Split
which is described as:
Splits a string into substrings based on specified delimiting characters.
Basically, there's no overload of Split
that takes a just a string
, so Windows PowerShell is trying to be helpful and finds the next best thing, which it decides is string[] Split(Params char[] separator)
because string
implements System.Collections.Generic.IEnumerable<char>
which means "target"
can be converted to @("t", "a", "r", "g", "e", "t")
to call that overload:
public sealed class String : ICloneable, IComparable, IComparable, IConvertible, IEquatable, System.Collections.Generic.IEnumerable
The result is that the string gets split at all instances of the individual characters in "target"
rather than the entire word as a whole.
If you want to split on the contiguous string "target"
you can... target ... the string[] Split(string[] separator, System.StringSplitOptions options)
overload instead as follows:
PS> "source target destination".ToLower().Split("target", "None")
source
destination
And if you want to see all the available overloads of Split
you can do this:
PS> "".Split
OverloadDefinitions
-------------------
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)
which just confirms there's no overload of the form string[] Split(string separator)
.
As an aside, if you want to trim the substrings you can do:
PS> "source target destination".ToLower().Split(@("target"), "None").Trim()
source
destination
which uses Member Access Enumeration to invoke Trim()
on all of the substrings.
PowerShell Core
PowerShell Core makes slightly different decisions about what overload of Split
to invoke because it has different overloads available in the underlying dotnet runtime compared to Windows PowerShell:
PS> "".Split
OverloadDefinitions
-------------------
string[] Split(char separator, System.StringSplitOptions options = System.StringSplitOptions.None)
string[] Split(char separator, int count, System.StringSplitOptions options = System.StringSplitOptions.None)
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 = System.StringSplitOptions.None)
string[] Split(string separator, int count, System.StringSplitOptions options = System.StringSplitOptions.None)
string[] Split(string[] separator, System.StringSplitOptions options)
string[] Split(string[] separator, int count, System.StringSplitOptions options)
The overload string[] Split(string separator, System.StringSplitOptions options = System.StringSplitOptions.None)
now has a default value of None
for the options
parameter which means PowerShell Core can bind to this overload instead and the result is:
PS> "source target destination".ToLower().Split("target")
source
destination
and as an aside again you can trim the values with this if you want:
PS> "source target destination".ToLower().Split(@("target"), "None").Trim()
source
destination
HTH.