Forums > MaxMSP

sprintf – padding with trailing spaces?

February 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?


February 27, 2010 | 12:43 pm

Here is a way:

– Pasted Max Patch, click to expand. –

February 27, 2010 | 4:35 pm

i believe sprintf symout %-9s will do what you want.


February 27, 2010 | 8:50 pm

Thanks, ‘sprintf symout %-9s’ does the trick.


March 1, 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?

– Pasted Max Patch, click to expand. –

March 1, 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].

lh


March 1, 2010 | 2:32 am

here is another possibility:

– Pasted Max Patch, click to expand. –

March 1, 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?


March 1, 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:

– Pasted Max Patch, click to expand. –

March 3, 2010 | 4:24 pm

@Peter, you can use tosymbol:

– Pasted Max Patch, click to expand. –

March 5, 2010 | 4:42 pm

Just another variant:

– Pasted Max Patch, click to expand. –

March 6, 2010 | 12:32 am

Wow, there’s quite a lot to choose from :-)
Many thanks guys!


March 6, 2010 | 9:23 am

Yes, that’s why its always fun for me to find yet another way of doing the same…;-)


Viewing 13 posts - 1 through 13 (of 13 total)