BRL.Retro
The BASIC compatibility module provides miscellaneous functions that emulate the behaviour of 'classic' BASIC.
The functions in this module have largely been superceded by BlitzMax features such as 'string slicing', and the Find, Replace, Trim, ToLower and ToUpper string methods.
However, for programmers from a classic BASIC background, these utility functions should make the transition to BlitzMax a little easier.
NOTE: Strings in classic BASIC are '1 based'. This means that the first character within a string is at index 1, the second at index 2 and so on. However, BlitzMax strings are '0 based', meaning the first character is at index 0, the second at index 1 and so on. The instr and Mid functions in this module retain the '1 based' behaviour of classic BASIC.
Functions
Function Mid$( str$,pos,size=-1 )
Extract substring from a string
The Mid$ command returns a substring of a String.
Given an existing string, a position from the start of the string and an optional size, Mid creates a new string equal to the section specified. If no size if given, Mid returns the characters in the existing string from position to the end of the string.
For compatibility with classic BASIC, the pos parameter is 'one based'.
Returns
A sequence of characters from str starting at position pos and of length size
Example
SuperStrict
Local a:String = "abcd1234efgh"
Print Mid(a,5,5) ' prints 1234e
Function Instr( str$,sub$,start=1 )
Find a string within a string
The start parameter allows you to specifying a starting index for the search.
For compatiblity with classic BASIC, the start parameter and returned position are both 'one based'.
Returns
The position within str of the first matching occurance of sub
Example
SuperStrict
Local mystring:String = "*sniffage*, I need more media!"
' check for the position
Print Instr(mystring,"more")
If Instr(mystring,"new PC") Print "large!"
If Not Instr(mystring,"new PC") Print "*sniff*"
If Instr(mystring,"media") Print "large!"
Function Left$( str$,n )
Extract characters from the beginning of a string
The Left$ command returns a substring of a String. Given an existing String and a size, Left$ returns the first size characters from the start of the String in a new String.
Returns
size leftmost characers of str
Example
SuperStrict
Print Left("12345678",4) ' prints 1234
Function Right$( str$,n )
Extract characters from the end of a string
The Right$ command returns a substring of a String. Given an existing String and a size, Right$ returns the last size characters from the end of the String.
Returns
size rightmost characters of str
Example
SuperStrict
Print Right("12345678",4) ' prints 5678
Function LSet$( str$,n )
Left justify string
Returns
A string of length n, padded with spaces
Example
SuperStrict
Print LSet("12345678",3)
Print "["+LSet("12345678",10)+"]"
' ==============
' Output
' 123
' [12345678 ]
Function RSet$( str$,n )
Right justify string
Returns
A string of length n, padded with spaces
Example
SuperStrict
Print RSet("12345678",3)
Print "["+RSet("12345678",10)+"]"
' ==============
' Output
' 678
' [ 12345678]
Function Replace$( str$,sub$,replaceWith$ )
Performs a search and replace function
The Replace$ command replaces all instances of one string with another.
Returns
A string with all instances of sub$ replaced by replace$
Example
SuperStrict
Local str:String = "This is a test of the Replace command."
Print "Original: "+str
str = Replace(str,"e","*")
Print "Altered: "+str
Function Trim$( str$ )
Remove unprintable characters from ends a string
Returns
str with leading and trailing unprintable characters removed
Function Lower$( str$ )
Convert string to lowercase
Returns
Lowercase equivalent of str
Example
SuperStrict
Print Lower("abcdEFGH") ' prints abcdefgh
Function Upper$( str$ )
Convert string to uppercase
Returns
Uppercase equivalent of str
Example
SuperStrict
Print Upper("Hello World") ' prints HELLO WORLD
Function Hex$( val )
Convert an integer value to a hexadecimal string
Returns
The hexadecimal string representation of val
Example
SuperStrict
For Local t:Int=0 To 255
If Not(t Mod 16) Print
Print "decimal: "+RSet(t,3)+" | hex: "+Hex(t)
Next
Function Bin$( val )
Convert an integer value to a binary string
Returns
The binary string representation of val
Example
' Prints the binary representation of a number
SuperStrict
Print Bin(1)
Print Bin(554)
Print Bin(1 | 554) ' OR in action
Function LongHex$( val:Long )
Convert a 64 bit long integer value to a hexadecimal string
Returns
The hexadecimal string representation of val
Function LongBin$( val:Long )
Convert a 64 bit long integer value to a binary string
Returns
The binary string representation of val