Shop OBEX P1 Docs P2 Docs Learn Events
String format function — Parallax Forums

String format function

LucidGuppyLucidGuppy Posts: 32
edited 2007-02-25 22:47 in Propeller 1
Right now I see two functions for making strings but there is no similar function to sprintf.
Where variables and strings can be put together. Or better yet to have a python like format function like...

" String with a %i variable " % (myVariable)

Is there any way to do this to the text objects without doing three function calls?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-02-25 20:40
    I'm not aware of anyone having written something like this yet. It's not possible to imbed the variable in the string since those names disappear when the program is compiled. It would be possible for someone to write a format string interpreter that would take a format string like "String with value %i and zero-terminated string %s", then pass it two values, one for each of the "%" items. You could even have extra entry points that would accept the string and some number of parameters like
    sprintf2(string("%i and %s"),5,string("X"))
    sprintf1(string("only one %i"),6)
    
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-02-25 21:11
    Awhile back I wrote a format object that does what you want. Here is a version of it using callbacks. As soon as I can locate the original, non-callback version, I'll post a link to that one, too — or you could just modify the callback version if you don't want to use it that way.

    -Phil
  • LucidGuppyLucidGuppy Posts: 32
    edited 2007-02-25 22:47
    Thanks a lot guys. I hope some of this code gets wrapped up into the propeller tool library.
Sign In or Register to comment.