我想用另一个文件替换文件中的第n个字符-例如,您拥有
println
我不知道该值是多少,但是我知道它在什么位置-因此在本例中为第6个字符。
尝试这样做时:
System.out.println(myHello.say);
它取代了预期出现的角色的每次出现,但并不能完全替代我想要的角色-我该如何解决?
答案 0 :(得分:0)
您的示例与问题描述不符。但是,根据您的评论,我认为您只是想将第26个字符替换为另一个。
为此,请以单个字符串形式读取文件并进行替换:
$charIndex = 26 # the character index. Remember this counts from 0
$replaceWith = 'x' # the character to put in the $charIndex place
Get-ChildItem "C:\Temp" -Filter "xfil*" -File | ForEach-Object {
$content = Get-Content -Path $_.FullName -Raw
$content.Replace($content[$charIndex], $replaceWith) | Set-Content -Path $_.FullName
}
替代
Get-ChildItem "C:\Temp" -Filter "xfil*" -File | ForEach-Object {
$content = Get-Content -Path $_.FullName -Raw
if ($content.Length -gt $charIndex) {
"{0}$replaceWith{1}" -f $content.Substring(0, $charIndex),
$content.Substring($charIndex + 1) |
Set-Content -Path $_.FullName
}
}
答案 1 :(得分:-1)
也可以使用这种衬纸来实现:
$Content = Get-Content -path $file.fullname
$Replace = $Content.Remove(22,1).Insert(22,$ReplaceNumber) | Set-content $file.fullname