STRING$ & SPACE$ functions

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

STRING$ & SPACE$ functions

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: STRING$ & SPACE$ functions

Post by rbytes »

I can see where these would have saved me a lot of time. Thanks, Dav!
The only thing that gets me down is gravity...

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: STRING$ & SPACE$ functions

Post by sarossell »

Much appreciated Dav. :)
smart BASIC Rocks!

- Scott : San Diego, California

User avatar
GeorgeMcGinn
Posts: 435
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro 10.5in
IMac
Linux i386
Windows 7 & 10
Location: Venice, FL
Flag: United States of America
Contact:

Re: STRING$ & SPACE$ functions

Post 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
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)

Post Reply