Jump to content

BlitzMax/Modules/BASIC/BASIC compatibility

From Wikibooks, open books for an open world

The BASIC compatibility module provides miscellaneous functions that emulate the behaviour of 'classic' BASIC.

The functions in this module have largely been superseded 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

[edit | edit source]

Function Mid$( str$,pos,size=-1 )

Description: Extract substring from a string

Returns: A sequence of characters from str starting at position pos and of length size

Information: 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'.

Instr

[edit | edit source]

Function Instr( str$,sub$,start=1 )

Description: Find a string within a string

Returns: The position within str of the first matching occurrence of sub

Information: The start parameter allows you to specifying a starting index for the search.

For compatibility with classic BASIC, the start parameter and returned position are both 'one based'.

Function Left$( str$,n )

Description: Extract characters from the beginning of a string

Returns: size leftmost characers of str

Information: 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.

[edit | edit source]

Function Right$( str$,n )

Description: Extract characters from the end of a string

Returns: size rightmost characters of str

Information: 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.

Function LSet$( str$,n )

Description: Left justify string

Returns: A string of length n, padded with spaces

Function RSet$( str$,n )

Description: Right justify string

Returns: A string of length n, padded with spaces

Replace

[edit | edit source]

Function Replace$( str$,sub$,replaceWith$ )

Description: Performs a search and replace function

Returns: A string with all instances of sub$ replaced by replace$

Information: The Replace$ command replaces all instances of one string with another.

Function Trim$( str$ )

Description: Remove unprintable characters from ends a string

Returns: str with leading and trailing unprintable characters removed

Lower

[edit | edit source]

Function Lower$( str$ )

Description: Convert string to lowercase

Returns: Lowercase equivalent of str

Upper

[edit | edit source]

Function Upper$( str$ )

Description: Convert string to uppercase

Returns: Uppercase equivalent of str

Function Hex$( val )

Description: Convert an integer value to a hexadecimal string

Returns: The hexadecimal string representation of val

Function Bin$( val )

Description: Convert an integer value to a binary string

Returns: The binary string representation of val

LongHex

[edit | edit source]

Function LongHex$( val:Long )

Description: Convert a 64 bit long integer value to a hexadecimal string

Returns: The hexadecimal string representation of val

LongBin

[edit | edit source]

Function LongBin$( val:Long )

Description: Convert a 64 bit long integer value to a binary string

Returns: The binary string representation of val