OK, now I understand (String/format "'%-9s'" (to-array "12345")) and 
(String/format 
"'%-9s'" (to-array '(\1 \2 \3 \4 \5))) both return '1 ' - because in each 
case the array returned from to-array contains individual characters and 
only the first gets formatted using the format string - but that doesn't 
explain why I'm not getting the full 9-character padding of '1        '.

It is also somewhat counter-intuitive as you expect clojure.core/format 
when using with a single string argument to format that single string, but 
in fact it treats the string as a collection of characters and so only 
applies the formatting to the first character. Hence the reason (String/format 
"'%-9s'" (to-array ["12345"])) works correctly.

Confusing?

On Sunday, 6 April 2014 13:27:09 UTC-6, Paul Umbers wrote:
>
> I'm trying to right-pad a string up to 9 characters, for example "12345" 
> should become "12345    ", and the only idea I've come up with so far is to 
> use clojure.core/format which states: "Formats a string using 
> java.lang.String.format, see java.util.Formatter for format string syntax
> ". However, there seems to be a discrepancy in what the Clojure format 
> function returns compared with the Java formatter.
>
> In Java, new Formatter().format("'%-9s'", "12345") correctly returns '12345 
>    ' - the ' are there for legibility. String.format("'%-9s'", "12345")also 
> returns the correctly padded string.
>
> However, in Clojure (format "'%-9s'" "12345") returns '12345 ' - it has 
> only padded the string by 1 space, and not 4 as with the Java version.
>
> Also, (String/format "'%-9s'" (to-array "12345")) - taken from the 
> clojure.core/format source and substituting parameters directly- returns '1 
>        ', and (String/format "'%-9s'" (to-array '(\1 \2 \3 \4 \5)))returns '1 
> '.
>
> The one thing that works correctly is (String/format "'%-9s'" (to-array 
> ["12345"])) which returns '12345    '.
>
> I'm not really clear on what's going wrong here, but clojure.core/format 
> doesn't appear to be working as described - at least for a single string 
> argument.
>
> I'll continue to investigate, but if anyone can offer some comments to 
> help? ...
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to