String manipulations

Discuss the Firewing language

String manipulations

Postby skartalov » Thu Nov 07, 2013 11:16 am

I have strings with different lengths in my database,
but I want when I print them to send exactly 20 characters, I need to add the required spaces an every string, so it becomes 20 chars in length.
I am sure it should be pretty easy, but how?

Thanks!
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm

Re: String manipulations

Postby David John Barker » Thu Nov 07, 2013 11:20 am

Something like this should help:
Code: Select all
// print a packed string...
sub PackPrint(str as string, length as byte, optional packChar as char = " ")
   console.write(str)
   for index as byte = len(str) + 1 to length
      console.write(packChar)
   next
   console.write(13,10)
end sub

// create a packed string - limited to 40 characters *including* null terminator...
function PackStr(str as string, length as byte, optional packChar as char = " ") as string(40)
   PackStr = str
   for index as byte = len(str) + 1 to length
      PackStr = PackStr + packChar
   next
end function

 // main program entry point...
 sub main()
   PackPrint("Hello", 20, "x")   // print and pack with "x"
   PackPrint("World", 20)        // print and pack with " "
                           
   // create a packed string and output...
   dim str as string = PackStr("Firewing", 20, ".")
   console.write(str,13,10)
end sub
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: String manipulations

Postby skartalov » Thu Nov 07, 2013 1:21 pm

It works! Thanks!
skartalov
 
Posts: 69
Joined: Sun Sep 15, 2013 1:12 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest

x