formatting strings

BlitzMax Forums/BlitzMax Programming/formatting strings

Zeke(Posted 2010) [#1]
This is a easy way to format strings using c sprintf() function.
if your string need only strings and integers then you can only use byte ptr as argumet type.
but if you need like string,int,float,string then you just need to extern function and set your arguments what you need.)
Example:
SuperStrict

Extern "c"
	Function sprintf(buffer:Byte Ptr , format$z , arg1:Byte Ptr = Null , arg2:Byte Ptr = Null , arg3:Byte Ptr = Null , argh4:Byte Ptr = Null) 'String and Int
	Function printfloat(buffer:Byte Ptr,format$z,arg1:Byte Ptr=Null,floatarg1:Double=Null,floatarg2:Double=Null)="sprintf" 'You need to use DOUBLE not float.
End Extern

Local outstr:Byte Ptr = MemAlloc(256) 'allocate memory to output string

Local name:String = "User Name"
Local age:Int = 32

sprintf(outstr , "Hello, %s, your age is %i." , name , Byte Ptr age) 'format string

Local mystr:String=String.FromCString(outstr)
Print mystr

Local myfloat:Float=Pi 
printfloat(outstr,"Hello, %s, Pi = %.3f",name,myfloat)

Local floatstr:String = String.FromCString(outstr)
Print floatstr

MemFree outstr



Jesse(Posted 2010) [#2]
nice Zeke. thanks


Abazek(Posted 2011) [#3]
Thats great, thanks! One of the things I really miss in BlitzMax is sprintf.

Last edited 2011