Cheries
Cheries

Reputation: 892

How to cut specific characters in a string and save it to a new file?

I have a string, and I want to cut some characters and store it to a new file.

I tried this code, but it still error.

 $a = ";Code=NB"
 $b = $a -split "="
 $b[1]
 $Save = "[AGM]", "CR=JP", "LOC= $b[1]"| Out-File "C:\Users\Out.txt"

Upvotes: 0

Views: 70

Answers (2)

Bacon Bits
Bacon Bits

Reputation: 32180

Try something like this:

$a = ";Code=NB"
$null, $b, $null = $a -split '=', 3
$b
$Save = "[AGM]", "CR=JP", "LOC= $b"| Out-File "C:\Users\Out.txt"

Upvotes: 2

Mark Harwood
Mark Harwood

Reputation: 2415

Something that would be easier to maintain would be this:

#Words to remove from string
$wordsToCut = "This","is"
#Phrase to remove words from
$phrase = "This is a test"
#Running through all words in words to remove
foreach ($word in $wordsToCut){
    #Replace current word with nothing
    $phrase = $phrase.Replace($word,"")
}
#Output end result
Write-Host $phrase

You would also use trim to remove any leading or trailing spaces. The above code outputs:

a test

Upvotes: 1

Related Questions