sprintf - padding with trailing spaces?

    Feb 27 2010 | 7:24 am
    I cannot figure out how to get right-padding for a string to reach a certain length. For example the string "boa", what do I tell [sprintf] to make it "boa......" –> 9 characters long with trailing spaces?

    • Feb 27 2010 | 12:43 pm
      Here is a way:
    • Feb 27 2010 | 4:35 pm
      i believe sprintf symout %-9s will do what you want.
    • Feb 27 2010 | 8:50 pm
      Thanks, 'sprintf symout %-9s' does the trick.
    • Mar 01 2010 | 1:27 am
      'sprintf symout %-9s' does not handle strings with spaces. When I send two words it outputs just one. I don't understand the C++ reference. At least I see no indication that spaces are handled different than other characters.
      Is there a solution?
    • Mar 01 2010 | 2:29 am
      Like most max objects [sprintf] will interpret a list as being sent one item to each inlet. Seeing as you only have one % argument in the [sprintf] object it will have one inlet and only use the first item in the list. The solution is to specify the input as being one symbol, either use quotes in the message box: "my pet". Or use [tosymbol] between the list and [sprintf].
    • Mar 01 2010 | 2:32 am
      here is another possibility:
    • Mar 01 2010 | 4:48 am
      like luke said, the problem here isn't sprintf but max. when you think you're sending a string with a space, you're probably sending a list of two strings. if you need to handle strings with whitespace, you'll need to use tosymbol in some places and quotation marks in others.
      actually, this is pretty much exactly what luke said. why did i even write this?
    • Mar 01 2010 | 6:14 am
      If you don't need extensibility, then sprintf is great, no need to go further.
      If you want to be able to change the size of the resulting string, you have to use something else. Here is some fun with the zl family:
    • Mar 03 2010 | 4:24 pm
      @Peter, you can use tosymbol:
    • Mar 05 2010 | 4:42 pm
      Just another variant:
    • Mar 06 2010 | 12:32 am
      Wow, there's quite a lot to choose from :-)
      Many thanks guys!
    • Mar 06 2010 | 9:23 am
      Yes, that's why its always fun for me to find yet another way of doing the same...;-)