sprintf – padding with trailing spaces?

Feb 27, 2010 at 7:24am

sprintf – padding with trailing spaces?

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?

#48806
Feb 27, 2010 at 12:43pm

Here is a way:

– Pasted Max Patch, click to expand. –
#175408
Feb 27, 2010 at 4:35pm

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

#175409
Feb 27, 2010 at 8:50pm

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

#175410
Mar 1, 2010 at 1:27am

‘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. –
#175411
Mar 1, 2010 at 2:29am

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

#175412
Mar 1, 2010 at 2:32am

here is another possibility:

– Pasted Max Patch, click to expand. –
#175413
Mar 1, 2010 at 4:48am

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?

#175414
Mar 1, 2010 at 6:14am

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. –
#175415
Mar 3, 2010 at 4:24pm

@Peter, you can use tosymbol:

– Pasted Max Patch, click to expand. –
#175416
Mar 5, 2010 at 4:42pm

Just another variant:

– Pasted Max Patch, click to expand. –
#175417
Mar 6, 2010 at 12:32am

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

#175418
Mar 6, 2010 at 9:23am

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

#175419

You must be logged in to reply to this topic.