PowerShell如何拆分连续的字符串。不是单个字母。

huangapple go评论76阅读模式
英文:

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.

英文:

powershell string split

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, &#39;None&#39;
&quot;source target destination&quot;.tolower().Split([string[]] &quot;target&quot;, &#39;None&#39;)

If you specify just &quot;target&quot;, 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:

&quot;source target destination&quot;.tolower().Split([char[]] &quot;target&quot;)

Note that &quot;source target destination&quot;.tolower().Split(&quot;target&quot;) 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: &#39;target&#39; is interpreted as a *regex* by default
&quot;source target destination&quot;.tolower() -split &#39;target&#39;

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.,
    &#39;a.b&#39; -split &#39;.&#39;, 0, &#39;SimpleMatch&#39;)

  • 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&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;)

等效于:

PS&gt; &quot;source target destination&quot;.ToLower().Split( @(&quot;t&quot;, &quot;a&quot;, &quot;r&quot;, &quot;g&quot;, &quot;e&quot;, &quot;t&quot;) )

因为 PowerShell 正在调用 Splitstring[] Split(Params char[] separator) 重载,该重载的描述如下:

> 根据指定的分隔字符将字符串拆分为子字符串。

基本上,没有接受仅仅是一个stringSplit重载,因此Windows PowerShell试图提供帮助,并找到了下一个最佳选择,它决定是string[] Split(Params char[] separator),因为string实现了 System.Collections.Generic.IEnumerable&lt;char&gt;,这意味着&quot;target&quot;可以转换为@(&quot;t&quot;, &quot;a&quot;, &quot;r&quot;, &quot;g&quot;, &quot;e&quot;, &quot;t&quot;)来调用该重载:

> public sealed class String : ICloneable, IComparable, IComparable<string>, IConvertible, IEquatable<string>, System.Collections.Generic.IEnumerable<char>

结果是字符串在&quot;target&quot;中的每个字符之间拆分,而不是作为整个单词

如果要在连续的字符串&quot;target&quot;上拆分,您可以... target ... string[] Split(string[] separator, System.StringSplitOptions options) 重载如下:

PS&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;, &quot;None&quot;)
source
 destination

如果您想查看所有可用的Split重载,可以执行以下操作:

PS&gt; &quot;&quot;.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&gt; &quot;source target destination&quot;.ToLower().Split(@(&quot;target&quot;), &quot;None&quot;).Trim()
source
destination

PowerShell Core

PowerShell Core对于要调用的Split重载做出了稍微不同的决策,因为它在底层的dotnet运行时中有不同的重载,与Windows PowerShell相比:

PS&gt; &quot;&quot;.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&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;)
source
 destination

另外,如果您想修剪值,可以使用以下方法:

PS&gt; &quot;source target destination&quot;.ToLower().Split(@(&quot;target&quot;), &quot;None&quot;).Trim()
source
destination

希望对您有所帮助。

英文:

Windows PowerShell

Your code sample:

PS&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;)

is equivalent to:

PS&gt; &quot;source target destination&quot;.ToLower().Split( @(&quot;t&quot;, &quot;a&quot;, &quot;r&quot;, &quot;g&quot;, &quot;e&quot;, &quot;t&quot;) )

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&lt;char&gt; which means &quot;target&quot; can be converted to @(&quot;t&quot;, &quot;a&quot;, &quot;r&quot;, &quot;g&quot;, &quot;e&quot;, &quot;t&quot;) 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 &quot;target&quot; rather than the entire word as a whole.

If you want to split on the contiguous string &quot;target&quot; you can... target ... the string[] Split(string[] separator, System.StringSplitOptions options) overload instead as follows:

PS&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;, &quot;None&quot;)
source
 destination

And if you want to see all the available overloads of Split you can do this:

PS&gt; &quot;&quot;.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&gt; &quot;source target destination&quot;.ToLower().Split(@(&quot;target&quot;), &quot;None&quot;).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&gt; &quot;&quot;.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&gt; &quot;source target destination&quot;.ToLower().Split(&quot;target&quot;)
source
 destination

and as an aside again you can trim the values with this if you want:

PS&gt; &quot;source target destination&quot;.ToLower().Split(@(&quot;target&quot;), &quot;None&quot;).Trim()
source
destination

HTH.

答案3

得分: 0

以下是要翻译的内容:

我认为下面的命令将产生你想要的结果:

&quot;source target destination&quot; | ForEach-Object { $_.Split(&#39; &#39;)[1] }

如果你想要逐个字符拆分单词"target",那么可以采用以下方法:

$res =&quot;source target destination&quot; | ForEach-Object { $_.Split(&#39; &#39;)[1] }
$res.ToCharArray()
英文:

I think below command will yield the result you are looking for:

&quot;source target destination&quot; | ForEach-Object { $_.Split(&#39; &#39;)[1] }

In case you want to split the word target character by character then below approach can be followed:

$res =&quot;source target destination&quot; | ForEach-Object { $_.Split(&#39; &#39;)[1] }
$res.ToCharArray() 

huangapple
  • 本文由 发表于 2023年5月13日 16:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241804.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定