Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 旋转变量中的文本行_String_Powershell_Variables_Rotation - Fatal编程技术网

String 旋转变量中的文本行

String 旋转变量中的文本行,string,powershell,variables,rotation,String,Powershell,Variables,Rotation,我有一个带序列号行的字符串变量,我需要旋转变量中的序列号,然后将其输出到屏幕上。 基本上,将第一个序列移到末尾,并从开头将其删除。 我能够将第一个序列移到变量的末尾,但无法确定如何从开头删除它 范例 $serials = "6546544`n6542185`n6546848`n6654898`n6522828" #append first serial number to the end $serials +="`n$($serials.split()[0])&qu

我有一个带序列号行的字符串变量,我需要旋转变量中的序列号,然后将其输出到屏幕上。 基本上,将第一个序列移到末尾,并从开头将其删除。 我能够将第一个序列移到变量的末尾,但无法确定如何从开头删除它

范例

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
#append first serial number to the end
$serials +="`n$($serials.split()[0])" 
#remove the first serial from beginning.????
最终结果应该是:

"6542185`n6546848`n6654898`n6522828`n6546544"

如果您愿意使用集合,队列对象本身就具有以下功能:

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
# Create queue object with entries for each serial
$queue = [System.Collections.Queue]::new(-split $serials)

# Dequeues the top item and enqueues the item to the bottom
$queue.Enqueue($queue.Dequeue())

# Create string joined by newline character
$serials = $queue -join "`n"

# Optionally output a list and skip the string join above
$queue
这会奏效的

$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
$test = @()
$test = $serials.split()
$final = @()

for($i = 1; $i -lt  $test.count; $i++){

    $final += $test[$i]

}

$final += $test[0]
$final

从我的评论扩展这个想法,不需要拆分

例如:

Clear-Host
Write-Verbose -Message "Current array" -Verbose
($serials = '6546544','6542185', '6546848', '6654898', '6522828')
"`n"
#append first serial number to the end
Write-Verbose -Message "Modified array" -Verbose
($serials += $($serials[0]))
"`n"
Write-Verbose -Message "Final array" -Verbose
($serials = ($serials | Select -Skip 1))
# Results
<#
VERBOSE: Current array
6546544
6542185
6546848
6654898
6522828


VERBOSE: Modified array
6546544
6542185
6546848
6654898
6522828
6546544


VERBOSE: Final array
6542185
6546848
6654898
6522828
6546544
#>

。。然后以您希望的任何方式重新连接。

另一种方法是将序列拆分为一个数组,并将该数组与附加到末尾的第一个元素重新组合:

# split on "`n" to get an array
$serials = "6546544`n6542185`n6546848`n6654898`n6522828" -split "`n"
# recombine the array in different order with "`n"
$serials = ($serials[1..($serials.Count - 1)] + $serials[0]) -join "`n"

$serials现在包含6542185`n6546848`n6654898`n6522828`n6546544

只是好奇而已。你为什么要这样做[6546544n6542185n6546848n6654898n6522828]而不是仅仅[6546544'、'6542185'、'6546848'、'6654898'、'6522828'?后者默认为and array/COLLATION,然后您只需使用普通的array add/remove或coolection add/remove方法等。您也可以使用$serials变量完成此操作。[$serials='6546544 6542185 6546848 6654898 6522828'-拆分“”],因为我没有创建此输入,它是从其他地方获取的。当返回输出时,它必须使用相同的格式。明白,但是使用格式化字符将其强制放入数组布局与使用自然数组相比,让我抓狂。
$serials = "6546544`n6542185`n6546848`n6654898`n6522828"
#append first serial number to the end
$first = ($serials.Split()[0]).trim()
$serials = ($serials -replace $first).trim()
$serials += "`n$($first)"