Removing a line separator

I am having a problem with line breaks in my data. The array was created using a string followed by -split. If you want to see the script part tell me.

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

Output:

 _ itemname_ 

Output Required:

 itemname 

I tried to embed:

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

Without any changes. Any ideas?

+6
source share
3 answers

Ok, I think this should work. It seemed to me that you need these underscores as a result.

 $array -replace "`n|`r" 
+8
source

Well, how about applying mjolinor code at the $ item level, for example:

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

Although I expect the same result that you already got, newlines are embedded in your line.

I cannot configure the same condition in $ array myself, maybe you could post this code.

It works?:

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

By default, the Get-Content command has a default delimiter for the new line "\ n". Create a costume parameter, and then run the replace command. Hope this helps.

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

All Articles