Section 3.12
Programming In QuickBASIC
3.12 String Processing
Strings are made up of a sequence of characters.
The ability to manipulate strings is vital for text processing, sorting data or
modifying string data that is input.
3.12.1 ASCII (American Standard Code for Information Interchange)
Each character has an ASCII code.
Character
|
ASCII Code
|
0
|
48
|
1
|
49
|
2
|
50
|
3
|
51
|
4
|
52
|
5
|
53
|
6
|
54
|
7
|
55
|
8
|
56
|
9
|
57
|
A
|
65
|
B
|
66
|
.
|
.
|
.
|
.
|
a
|
97
|
b
|
98
|
.
|
.
|
.
|
.
|
z
|
122
|
The ASC
Function
The ASC
function returns the ASCII value of a given character.
E.g. PRINT ASC("a")
displays 97
The CHR$
Function
The CHR$
function returns a character equivalent to a given ASCII
code.
E.g. PRINT CHR$(97)
displays a
3.12.2 INKEY$
The INKEY$ function can be used to get the value of a single character.
When QuickBASIC encounters an expression containing the INKEY$ function it
checks to see if the user has pressed a key since:
- the last INKEY$ statement.
- the beginning of the program.
If so, INKEY$ returns that character otherwsie it returns a null string
3.12.3 Concatenating Strings
Two strings can be combined with the + operator.
A$ = "first"
B$ = "second"
C$ = A$ + " " + B$
PRINT C$
displays: first string
It is possible to convert an integer expression to a string expression and
vice-versa.
The STR$
Function
The STR$
function takes a integer and returns a string.
num$ = STR$(23)
The VAL
Function
The VAL
function takes a string and returns a integer.
num$ = STR$(23)
The LEN
function can be used in order to find out how many
characters are in a string.
PRINT LEN("Havant")
displays: 6
It is possible to remove trailing or leading spaces.
RTRIM$(any$) - removes any spaces at the end of any$.
LTRIM$(any$) - removes any spaces at the start of any$.