Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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/9/ruby-on-rails-3/4.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/8/file/3.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
Powershell 更改重复字符串中1个字符的值_Powershell - Fatal编程技术网

Powershell 更改重复字符串中1个字符的值

Powershell 更改重复字符串中1个字符的值,powershell,Powershell,全部。我一直在拼命想弄明白这件事 假设我有一个数组,比如 $array2 = [System.Collections.ArrayList]::new() 它有一个名为Misc $array2[$x].Misc 此变量包含字符串“----” 根据程序所做的,我想用不同的数字和字母来更新特定的空格 所以如果它是X,我希望它说“--X-” 或者如果它是y,我希望它说“-y----” 我能找到的唯一答案是使用string.replace,但如果字符串中的所有字符都相同,我不知道如何使用它。我试着把

全部。我一直在拼命想弄明白这件事

假设我有一个数组,比如

$array2 = [System.Collections.ArrayList]::new() 
它有一个名为
Misc

$array2[$x].Misc
此变量包含字符串
“----”

根据程序所做的,我想用不同的数字和字母来更新特定的空格

所以如果它是X,我希望它说
“--X-”

或者如果它是y,我希望它说
“-y----”


我能找到的唯一答案是使用string.replace,但如果字符串中的所有字符都相同,我不知道如何使用它。我试着把它做成一个字符数组,然后把它连在一起,但我似乎无法让它工作。

你可以用正则表达式替换某个字符

'-----' -replace '(?<=^.{2}).{1}','X'

--X--
这是一个伟大的时间,使它成为一个功能

Function Set-String {
    Param(
        [string]$InputObject,
        [char]$NewChar,
        [int]$Index
    )

    $after = $Index - 1

    $pattern = "(?<=^.{$after}).{1}"

    $InputObject -replace $pattern,$NewChar

}
但是如果你想替换2个字符,你必须调用它两次。相反,让我们改进函数以允许替换多个字符。还添加了一些错误处理

Function Set-String {
    Param(
        [string]$InputObject,
        [string]$NewText,
        [int]$Index
    )

    $after = $Index - 1

    if( $InputObject.Length -lt $NewText.Length ){
        Write-Warning "Replacement text is longer than the input string"
        break
    }
    elseif( ($NewText.Length + $after) -gt $InputObject.Length ){
        Write-Warning "Resulting string would be longer than the input string"
        break
    }

    $pattern = "(?<=^.{$after}).{$($NewText.length)}"

    $InputObject -replace $pattern,$NewText

}
或者,如果确实将该属性设置为数组而不是字符串,则可以简单地使用
-join
将其再次设置为字符串,然后随意替换元素

$array = '-----'.ToCharArray()

-join $array

-----

$array[1] = 'X'

-join $array

-X---

$array[1] = '-'
$array[2] = 'Y'

-join $array

--Y--

对于
$array='-'、'-'、'-'、'-'、'-'、'-'、'-'
,我将使用:
$array='-'.tocharray()
,以问题中给定的字符串开始。我同意,可以随意更改它。否则我很快就会换的。电话一直在合二为一。谢谢你的帮助!我决定暂时使用顶部的正则表达式,但很快就会把它变成一个函数。:)
Function Set-String {
    Param(
        [string]$InputObject,
        [string]$NewText,
        [int]$Index
    )

    $after = $Index - 1

    if( $InputObject.Length -lt $NewText.Length ){
        Write-Warning "Replacement text is longer than the input string"
        break
    }
    elseif( ($NewText.Length + $after) -gt $InputObject.Length ){
        Write-Warning "Resulting string would be longer than the input string"
        break
    }

    $pattern = "(?<=^.{$after}).{$($NewText.length)}"

    $InputObject -replace $pattern,$NewText

}
Set-String -InputObject '-----' -Index 2 -NewText X

-X---

Set-String -InputObject '-X---' -Index 2 -NewText -Y

--Y--

Set-String -InputObject '-X-Y-' -Index 3 -NewText XX

-XXX-
$array = '-----'.ToCharArray()

-join $array

-----

$array[1] = 'X'

-join $array

-X---

$array[1] = '-'
$array[2] = 'Y'

-join $array

--Y--