英文:
How does powershell split consecutive strings. Not a single letter
问题
In this picture, PowerShell has split each letter of "target," but you want the whole string "target" to be split. To achieve that, you can use the String.Split()
method with an array containing the word "target" as a single element. Here's the modified code:
PS C:\Users\Administrator> "source target destination".ToLower().Split(@("target"))
source
destination
This will split the string at "target" as a whole word. If you tried using regular expressions without success, this method should work for your purpose.
英文:
In this picture, powerhsell has split each letter of target, but I really want the string target as a whole to be split. How do I do that?
powershell
PS C:\Users\Administrator> "source target destination".tolower().Split("target")
sou
c
d
s
in
ion
I tried using regular expressions without success. Do you have a solution?
答案1
得分: 3
以下是翻译好的部分:
"The implication is that you're using Windows PowerShell, which is built on .NET Framework, where the [string]
type's .Split()
method has no overload for a single string argument constituting the desired separator."
"Such functionality is only available via a string array, which then also requires specifying options:"
# Required in Windows PowerShell
# Note the [string[]] cast, and the required options argument, 'None'
"source target destination".tolower().Split([string[]] "target", 'None')
"If you specify just "target"
, as in your attempt, Windows PowerShell chooses the char[]
overload, i.e. it splits your string into an array of characters of which each is treated as a separator - which explains the symptom you saw."
"That is, in Windows PowerShell your attempt is equivalent to:"
"source target destination".tolower().Split([char[]] "target")
"Note that "source target destination".tolower().Split("target")
does work the way you want in PowerShell (Core) 7+, where the underlying .NET (Core) runtime now does have an overload that accepts a single [string]
(with an optional second parameter for the splitting options)."
"Taking a step back:"
"The following works in both PowerShell editions:"
# Note: 'target' is interpreted as a *regex* by default
"source target destination".tolower() -split 'target'
"That is, use of the PowerShell-native -split
operator avoids the pitfall of changes in the underlying .NET framework resulting in changes that are outside of PowerShell's control (the only way to avoid them is to always cast method arguments to their exact type)."
"Note that -split
:
-
by default treats its RHS operand as a regex, which makes it more flexible than the
.Split()
method; a literal-string opt-in is available (e.g., 'a.b' -split '.', 0, 'SimpleMatch') -
is case-insensitive, as PowerShell generally is (unlike .NET); use the
-csplit
variant for case-sensitive matching."
"The pitfall at hand, along with -split
's enhanced capabilities, make it advisable to prefer -split
over .Split()
in general and, more generally - where feasible - choose PowerShell operators over .NET method calls for long-term stability. See this answer for a detailed discussion."
英文:
<!-- language-all: sh -->
The implication is that you're using Windows PowerShell, which is built on .NET Framework, where the [string]
type's .Split()
method has no overload for a single string argument constituting the desired separator.
Such functionality is only available via a string array, which then also requires specifying options:
# Required in Windows PowerShell
# Note the [string[]] cast, and the required options argument, 'None'
"source target destination".tolower().Split([string[]] "target", 'None')
If you specify just "target"
, as in your attempt, Windows PowerShell chooses the char[]
overload, i.e. it splits your string into an array of characters of which each is treated as a separator - which explains the symptom you saw.
That is, in Windows PowerShell your attempt is equivalent to:
"source target destination".tolower().Split([char[]] "target")
Note that "source target destination".tolower().Split("target")
does work the way you want in PowerShell (Core) 7+, where the underlying .NET (Core) runtime now does have an overload that accepts a single [string]
(with an optional second parameter for the splitting options).
Taking a step back:
The following works in both PowerShell editions:
# Note: 'target' is interpreted as a *regex* by default
"source target destination".tolower() -split 'target'
That is, use of the PowerShell-native -split
operator avoids the pitfall of changes in the underlying .NET framework resulting in changes that are outside of PowerShell's control (the only way to avoid them is to always cast method arguments to their exact type).
Note that -split
:
-
by default treats its RHS operand as a regex, which makes it more flexible than the
.Split()
method; a literal-string opt-in is available (e.g.,
'a.b' -split '.', 0, 'SimpleMatch'
) -
is case-insensitive, as PowerShell generally is (unlike .NET); use the
-csplit
variant for case-sensitive matching.
The pitfall at hand, along with -split
's enhanced capabilities, make it advisable to prefer -split
over .Split()
in general and, more generally - where feasible - choose PowerShell operators over .NET method calls for long-term stability. See this answer for a detailed discussion.
答案2
得分: 3
以下是您要翻译的内容:
Windows PowerShell
您的代码示例:
PS> "source target destination".ToLower().Split("target")
等效于:
PS> "source target destination".ToLower().Split( @("t", "a", "r", "g", "e", "t") )
因为 PowerShell 正在调用 Split
的 string[] Split(Params char[] separator)
重载,该重载的描述如下:
> 根据指定的分隔字符将字符串拆分为子字符串。
基本上,没有接受仅仅是一个string
的Split
重载,因此Windows PowerShell试图提供帮助,并找到了下一个最佳选择,它决定是string[] Split(Params char[] separator)
,因为string
实现了 System.Collections.Generic.IEnumerable<char>
,这意味着"target"
可以转换为@("t", "a", "r", "g", "e", "t")
来调用该重载:
> public sealed class String : ICloneable, IComparable, IComparable<string>, IConvertible, IEquatable<string>, System.Collections.Generic.IEnumerable<char>
结果是字符串在"target"
中的每个字符之间拆分,而不是作为整个单词。
如果要在连续的字符串"target"
上拆分,您可以... target ... string[] Split(string[] separator, System.StringSplitOptions options)
重载如下:
PS> "source target destination".ToLower().Split("target", "None")
source
destination
如果您想查看所有可用的Split
重载,可以执行以下操作:
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)
这只是确认没有形式为string[] Split(string separator)
的重载。
另外,如果您想修剪子字符串,可以执行以下操作:
PS> "source target destination".ToLower().Split(@("target"), "None").Trim()
source
destination
PowerShell Core
PowerShell Core对于要调用的Split
重载做出了稍微不同的决策,因为它在底层的dotnet运行时中有不同的重载,与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)
现在,string[] Split(string separator, System.StringSplitOptions options = System.StringSplitOptions.None)
重载的options
参数有一个默认值None
,这意味着PowerShell Core可以绑定到这个重载,结果是:
PS> "source target destination".ToLower().Split("target")
source
destination
另外,如果您想修剪值,可以使用以下方法:
PS> "source target destination".ToLower().Split(@("target"), "None").Trim()
source
destination
希望对您有所帮助。
英文:
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<string>, IConvertible, IEquatable<string>, System.Collections.Generic.IEnumerable<char>
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.
答案3
得分: 0
以下是要翻译的内容:
我认为下面的命令将产生你想要的结果:
"source target destination" | ForEach-Object { $_.Split(' ')[1] }
如果你想要逐个字符拆分单词"target",那么可以采用以下方法:
$res ="source target destination" | ForEach-Object { $_.Split(' ')[1] }
$res.ToCharArray()
英文:
I think below command will yield the result you are looking for:
"source target destination" | ForEach-Object { $_.Split(' ')[1] }
In case you want to split the word target character by character then below approach can be followed:
$res ="source target destination" | ForEach-Object { $_.Split(' ')[1] }
$res.ToCharArray()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论