Forum Discussion

dkrabo's avatar
dkrabo
Copper Contributor
Feb 07, 2022

Masking Sentences

what is the best way to get the result of  a** d** g** from input of 'abc def ghi'? with the least performance hit.

 

$test = 'abc def ghi'

((($test -split ' ')).substring(0,1) ) -join ' '

 

not sure how to add the ** or even a single * will be good. I need to do this to quite a big data, so I need it to run as fast as you can without using the | on each value...

  • dkrabo 

    Ok, created a file containing this to test on (d:\temp\x.txt in my case) 

     

    abc def ghi
    jkl mno pqr
    stu vwx yz

     

    If I let this script run on it:

    foreach ($line in get-content d:\temp\x.txt) {
    (((($line -split ' ')).substring(0,1) ) -join ' ').Replace(' ','** ') | foreach {$_ + '**'}
    }

    This is the output:
    a** d** g**
    j** m** p**
    s** v** y**

Resources