command or function to write specified character specified number of times?

Answered command or function to write specified character specified number of times?

  • Tuesday, November 24, 2009 8:24 PM
     
     
    Hello,

    what are some options for writing a specified character a specified number of times?

    to clarify, say I have a column width of 50 and each item I print will be less that 50 characters, but I want to fill any remaining space of that 50 that my item does not fill with a character, like a space or '-''s. For purposes of output formatting.

    within some loop structure I envision something like this:

    write-host $myItem pad('-', 50 - $myItem.length) $myValue

    resulting in something like this:

    Item1 ---------------- value1
    item2 ---------------- value2
    AnotheItem --------- anotherValue
    another -------------- another value

All Replies

  • Tuesday, November 24, 2009 9:50 PM
     
     Answered
    You're on the money.  A string in powershell is a .net implementation of a string so you can invoke any methods that you can in .net:
    http://msdn.microsoft.com/en-us/library/36f2hz3a.aspx

    $item = 'Item1'
    $value = 'value1'
    Write-host $item.padright(50,'-')$value
    • Proposed As Answer by MOW_ Tuesday, November 24, 2009 9:59 PM
    • Marked As Answer by c0pe Wednesday, November 25, 2009 1:43 AM
    •  
  • Tuesday, November 24, 2009 11:45 PM
    Moderator
     
     Answered
    write-host $myItem pad('-', 50 - $myItem.length) $myValue


    How are you creating your variables?

    Here's one method:
    ${first}+"-"*(50-${first}.length)+${second}
    • Marked As Answer by c0pe Wednesday, November 25, 2009 1:43 AM
    •  
  • Wednesday, November 25, 2009 12:16 AM
     
     
    Hey, that's neat. I didn't realize you can multiply strings.

    I was a little confused at first.... I thought ${first} was some kind of special variable, until I realized you meant something like this:
    $item = 'Item1'
    $value = 'value1'
    $item+"-"*(50-$item.length)+$value
    • Edited by Tome TanasovskiMVP Wednesday, November 25, 2009 12:18 AM posted before finished typing
    •  
  • Wednesday, November 25, 2009 12:20 AM
     
     Answered
    To correct my original post....  To get it without an extra space at the end of the padding you would need to do something like this:
    $item = 'Item1'
    $value = 'value1'
    write-host ($item.padright(50,'-')+$value)
    • Marked As Answer by c0pe Wednesday, November 25, 2009 1:44 AM
    •  
  • Wednesday, November 25, 2009 12:28 AM
    Moderator
     
     
    Hey, that's neat. I didn't realize you can multiply strings.

    I was a little confused at first.... I thought ${first} was some kind of special variable, until I realized you meant something like this:
    $item = 'Item1'
    $value = 'value1'
    $item+"-"*(50-$item.length)+$value

    Yeah, the "{}" was just remnants of me first trying a different method, which didn't work.  They aren't really required in my case as you've shown...
  • Wednesday, November 25, 2009 1:45 AM
     
     
    awesome, thank you both.