none
Help with more efficient code! RRS feed

  • Question

  • Hi team,

    I have an efficiency opprtunity....I am super new, and still getting my legs under me in terms of powershell.  

    cls
    #Some string
    $string = "Lorem and ipsum or dolor or sit amet, consectetur ad ipiscing and elit."
    
    #Remove the "," from the string
    $Pass1 = $string.replace(',',"")
    
    #remove the periods from the string
    $Pass2 = $Pass1.replace('.',"")  
    
    $pass2
    

    The result is what I want: Lorem and ipsum or dolor or sit amet consectetur ad ipiscing and elit

    My Question:  Can I do this in less steps?

    Wednesday, October 23, 2019 3:31 PM

Answers

  • Yes - remove al unnecessary comments and use the correct operators.

    $string = "Lorem and ipsum or dolor or sit amet, consectetur ad ipiscing and elit."
    $string -replace ',|\.'
    

    Step back and learn PowerShell formally via a good book or a tutorial.  Also rad all of the following document:

    The PowerShell Best Practices and Style Guide

    This will save you time and confusion while you are trying to learn PowerShell.


    \_(ツ)_/

    Wednesday, October 23, 2019 3:49 PM

All replies