string PowerShell 在第一次出现子字符串/字符时拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25383263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerShell Split a String On First Occurrence of Substring/Character
提问by DarkLite1
I have a string that I want to split up in 2 pieces. The first piece is before the comma (,
) and the second piece is all stuff after the comma (including the commas).
我有一个字符串,我想分成两部分。第一段在逗号 ( ,
)之前,第二段是逗号之后的所有内容(包括逗号)。
I already managed to retrieve the first piece before the comma in the variable $Header
, but I don't know how to retrieve the pieces after the first comma
in one big string.
我已经设法在变量中检索逗号之前的第一个片段$Header
,但我不知道如何检索comma
一个大字符串中第一个之后的片段。
$string = "Header text,Text 1,Text 2,Text 3,Text 4,"
$header = $string.Split(',')[0] # $Header = "Header text"
$content = "Text 1,Text 2,Text 3,Text 4,"
# There might be more text then visible here, like say Text 5, Text 6, ..
回答by Aaron Jensen
PowerShell's -split
operator supports specifying the maximum number of sub-strings to return, i.e. how many sub-strings to return. After the pattern to split on, give the number of strings you want back:
PowerShell 的-split
运算符支持指定要返回的最大子字符串数,即要返回多少个子字符串。在要拆分的模式之后,给出您想要返回的字符串数:
$header,$content = "Header text,Text 1,Text 2,Text 3,Text 4," -split ',',2
回答by Aserre
Try something like :
尝试类似:
$Content=$String.Split([string[]]"$Header,", [StringSplitOptions]"None")[1]
As you split according to a String, you are using a different signature of the function split
.
当您根据 String 进行拆分时,您使用的是函数的不同签名split
。
The basic use needs only 1 argument, a separator character (more info about it can be found here, for instance). However, to use strings, the signature is the following :
基本用法只需要 1 个参数,一个分隔符(例如,可以在此处找到有关它的更多信息)。但是,要使用字符串,签名如下:
System.String[] Split(String[] separator, StringSplitOptions options)
This is why you have to cast your string as an array of string. We use the None
option in this case, but you can find the other options available in the split
documentation.
这就是为什么您必须将字符串转换为字符串数组的原因。我们None
在这种情况下使用该选项,但您可以在split
文档中找到其他可用选项。
Finally, as the value of $Heasder,
is at the beggining of your $String
, you need to catch the 2nd member of the resulting array.
最后,由于 的值$Heasder,
在 your 的开头$String
,您需要捕获结果数组的第二个成员。
回答by Esperento57
method of Aaron is the best, but i propose my solution
Aaron 的方法是最好的,但我提出了我的解决方案
$array="Header text,Text 1,Text 2,Text 3,Text 4," -split ','
$array[0],($array[1..($array.Length -1)] -join ",")
回答by Josh
This alternate solution makes use of PowerShell's ability to distribute arrays to multiple variables with a single assignment. Note, however, that the -split
operator splits on every comma and PowerShell's built-in conversion from Array back to String results in the elements being concatenated back together. So it's not as efficient as String.Split
, but in your example, it's negligible.
此替代解决方案利用 PowerShell 的功能,通过一次赋值将数组分配给多个变量。但是请注意,-split
运算符在每个逗号上进行拆分,PowerShell 的内置从 Array 转换回 String 会导致元素重新连接在一起。所以它不如 高效String.Split
,但在你的例子中,它可以忽略不计。
$OFS = ','
$Content = 'Header text,Text 1,Text 2,Text 3,Text 4,'
[String]$Header,[String]$Rest = $Content -split $OFS
$OFS = ' '
Write-Host "Header = $Header"
Write-Host "Rest = $Rest"
Finally, $OFS
is a special variable in PowerShell that determines which character will be used when joining the array elements back into a single string. By default, it's a space. But it can be changed to anything.
最后,$OFS
是 PowerShell 中的一个特殊变量,用于确定将数组元素连接回单个字符串时将使用哪个字符。默认情况下,它是一个空格。但它可以更改为任何内容。