문제

I am having an issue with a line break in my data. The array was made with an out-string followed by -split. If you want to see that part of the script let me know.

foreach ($item in $array) {
"_"+$item+"_"
}

Output:

_
itemname_

Desired Output:

itemname

I've tried inserting:

$item.replace('`','')

Without any change. Any ideas?

도움이 되었습니까?

해결책

Okay, I think this should work. I was under the impression you wanted those underscores in the result.

$array -replace "`n|`r"

다른 팁

By default, 'Get-Content' command has the default delimiter of a new line '\n'. Create a costume parameter and then do your replace command. Hope this helps.

Get-ChildItem | Get-Content -Delimiter "~" | foreach { $_ -replace "`r|`n","" }

Well how about applying mjolinor's code at the $item level, e.g.:

foreach ($item in $array) {
  $item -replace '^|$','_'
}

Although I expect the same result you are already getting, there are newlines embedded in your string.

I'm not able to setup the same condition in $array myself, maybe you could post that code.

Does this work?:

foreach ($item in $array) {
  $item.Trim() -replace '^|$','_'
}
foreach ($item in $array) {
"_"+$item.Trim()+"_"
}

Should do what you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top