Page 1 of 1

STRING$ & SPACE$ functions

Posted: Thu Sep 24, 2015 2:28 pm
by Dav
Simple STRING$ & SPACE$ functions like those found in many other basic languages. They are simple to make & use, and came in handy for a couple of programs I made.

- Dav

Code: Select all


'string&space.txt
'STRING$ and SPACE$ functions for sB
'by Dav SEP/2015


a$=string$(10,"x")

b$=space$(10)

print a$&b$&string$(10,"o")

print string$(22,"C")



def string$(num, a$)
'returns string of a$ based on num
'example, print string$(0,"x") makes...
'xxxxxxxxxx

  b$=""
  for g=1 to num
    b$=b$&a$
  next g
  return b$
end def


def space$(num)
'returns string of spaces based on num
'example, a$=space$(100) makes a string
'containing 100 spaces.

  b$=""
  for g=1 to num
    b$=b$&" "
  next g
  return b$
end def

Re: STRING$ & SPACE$ functions

Posted: Thu Sep 24, 2015 7:09 pm
by rbytes
I can see where these would have saved me a lot of time. Thanks, Dav!

Re: STRING$ & SPACE$ functions

Posted: Wed Dec 14, 2016 11:55 pm
by sarossell
Much appreciated Dav. :)

Re: STRING$ & SPACE$ functions

Posted: Thu Dec 15, 2016 6:51 am
by GeorgeMcGinn
Thanks for sharing this Dav, as I find it very useful and will add it to my library "System Library.lib" with all the other functions I use regularly.

I have coded two groups of code that I am in the process of creating them as functions. They are STRING and UNSTRING statements.

As a COBOL programmer (among the close to 50 programming languages I can or have at some time written programs in in my 40+ years, I relied heavily on two string commands: STRING and UNSTRING. And in COBOL, they are very powerful.

UNSTRING does exactly what SmartBASIC's SPLIT and SPLITE commands. STRING does not have a SmartBASIC counterpart, so I wrote my own routine to do this.

Right now the code I have in one of my programs (it takes a JSON file and creates a PIPE delimited file, is executed inline during program execution. However, I will create them as functions and post them here.

I find these two statements from COBOL very useful and powerful when dealing with input that is delimited by one or more symbols, like PIPES "|" or: :;\/," " and TAB.

UNSTRING takes a file delimited by one or more separators and puts them in fields. The statement's parameters let you do this by SIZE, SPACE, etc. This means that it uses the size of the field as the ending point as well as the SPACE in the output variables.

STRING does the opposite. It takes a list of variables, and based on parameters for DELIMITER and others, will take a list of variables and using some of the same parameters like "DELIMITED BY SIZE" or "DELIMITED BY SPACE" creates a delimited record for output to a file.

If interested, i will share the two functions after I finish my coding of them.

George.

Dav wrote:Simple STRING$ & SPACE$ functions like those found in many other basic languages. They are simple to make & use, and came in handy for a couple of programs I made.

- Dav

Code: Select all


'string&space.txt
'STRING$ and SPACE$ functions for sB
'by Dav SEP/2015


a$=string$(10,"x")

b$=space$(10)

print a$&b$&string$(10,"o")

print string$(22,"C")



def string$(num, a$)
'returns string of a$ based on num
'example, print string$(0,"x") makes...
'xxxxxxxxxx

  b$=""
  for g=1 to num
    b$=b$&a$
  next g
  return b$
end def


def space$(num)
'returns string of spaces based on num
'example, a$=space$(100) makes a string
'containing 100 spaces.

  b$=""
  for g=1 to num
    b$=b$&" "
  next g
  return b$
end def