Thursday, 3 June 2010

Replace-String2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script implements an MSDN sample in PowerShell 
  4. .DESCRIPTION 
  5.     This script uses the String Replace method to replace 
  6.     all occurances of one char in a string to another.  
  7. .NOTES 
  8.     File Name  : Replace-String2.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com/2010/06/replace-string2ps1.html 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/czx8s9ts%28VS.80%29.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Replace-String2.ps1' 
  18.     Original string: "1 2 3 4 5 6 7 8 9" 
  19.     CSV string:      "1,2,3,4,5,6,7,8,9" 
  20. #> 
  21.  
  22. ##
  23. # Start of Script
  24. ##
  25.   
  26. # Create string 
  27. $Str = "1 2 3 4 5 6 7 8 9" 
  28.   
  29. # Display Original String 
  30. "Original string: `"{0}`"" -f $Str 
  31.   
  32. # Display new string, replacing " " with "," 
  33. "CSV string:      `"{0}`"" -f $Str.Replace(' ', ','
  34. # End of script 
  35. # Display Original String 
  36. "Original string: `"{0}`"" -f $Str 
  37.   
  38. # Display new string, replacing " " with "," 
  39. "CSV string:      `"{0}`"" -f $Str.Replace(' ', ','
  40. # End of script

No comments: