Page 1 of 1

String manipulations

PostPosted: Thu Nov 07, 2013 11:16 am
by skartalov
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!

Re: String manipulations

PostPosted: Thu Nov 07, 2013 11:20 am
by David John Barker
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

Re: String manipulations

PostPosted: Thu Nov 07, 2013 1:21 pm
by skartalov
It works! Thanks!