String Library Functions
Introduction
In programming, string manipulation is an important skill for efficiently managing text data. QBASIC, an early but enduringly popular programming language, includes several built-in string handling functions. Understanding string library functions is critical whether you're a seasoned developer returning to QBASIC or a newcomer eager to learn about its features. In this guide, we'll look at the core string functions available in QBASIC and provide practical examples and tips for using them in your code.
What are the String Library Functions in QBASIC?
QBASIC's string library functions are a set of built-in tools for performing various string operations. These functions enable programmers to effectively manipulate, search, and format text. Understanding these functions can improve your ability to work with text-based data, ranging from simple string concatenation to complex pattern searching.
LEN
This
function returns the length of a string expression including a blank space.
Syntax:
LEN(string expression)
OR
LEN(variable)
Where,
String
expression is a string constant
Variable
is a string variable
REM
Example LEN function CLS PRINT
LEN("QBASIC") a$
= "PROGRAMMING" l
= LEN(a$) PRINT
l END |
Output |
LEFT$
This
function allows us to extract the specific number of characters beginning from
the left-most character of the string.
Syntax:
LEFT$(string expression, n)
Where,
String
expression is a string constant or string
variable.
n
is the specifies number of characters to be extracted from left- hand side.
(Ranges from 0-32767)
REM Example of LEFT$ function. CLS PRINT
LEFT$("QBASIC", 2) a$ = "PROGRAMMING" PRINT LEFT$(a$, 4) END |
Output |
REGHT$
This
function allows us to extract the specific number of characters beginning from
the right-most character of the string.
Syntax:
RIGHT$(string expression, n)
Where,
String
expression is a string constant or string
variable.
n
is the specifies number of characters to be extracted from right-hand side.
(Ranges from 0-32767)
REM Example of LEFT$ function. CLS PRINT
RIGHT$("QBASIC", 5) a$ = "PROGRAMMING" PRINT RIGHT$(a$, 6) END |
Output |
MID$
This
function is used to returns a specific number of characters from a string.
Syntax:
MID$(string expression, start, length)
Where,
String
expression is the string constant or variable.
Start
is the position measured from the lest-most character.
Length
is the number of characters to extract.
REM Example of LEFT$ function. CLS PRINT MID$("QBASIC",
2, 3) a$ = "PROGRAMMING" PRINT MID$(a$, 4, 4) END |
Output |
LCASE$
This
function converts given uppercase string in lowercase (small letter).
Syntax:
LCASE$(string expression)
Where,
String
expression can be a string constant or string
variable.
Example:
REM Example of LCASE$
function. CLS PRINT LCASE$(“AVN”) END |
Output avn |
UCASE$
This
function converts given lowercase string into uppercase (capital letter)
Syntax:
UCASE$(string expression)
Where,
String
expression can be a string constant or string
variable.
Example:
REM Example of UCASE$
function. CLS PRINT UCASE$(“nepal”) END |
Output NEPAL |
This
function returns ASCII value of the first character of the given string.
ASCII:
American Standard Code for Information Interchange
Following
are the some common ASCII codes
ASCII codes |
Characters |
65
to 90 |
A
to Z in capital letters |
97
to 122 |
a
to z in small letters |
48
to 57 |
0
to 9 numbers |
0
to 31 |
Control
characters |
32
to 64 |
Punctuation
and mathematical symbols |
91
to 96 and 123 to 127 |
Miscellaneous
symbols |
Where,
String
expression can be string constant or string
variable.
Example:
REM Example of ASC function. CLS PRINT ASC(“AVN”) PRINT ASC (“a”) END |
Output 65 97 |
This
function is used to converts ASCII code into character.
Syntax:
CHR$(numeric expression)
Where,
Numeric
expression is any number of variable within the character code range from 0 to 255.
Example:
REM Example of CHR$ function. CLS PRINT CHR$(66) PRINT CHR$(98) PRINT CHR$(2) END |
|
STRING$
This
function is used to return a string of specified length by repeating the
particular characters whose ASCII code or first character of string expression
is given.
Syntax:
STRING$(length, ASCII code/string expression)
Where,
Length
is a numeric expression to print a character for a specified number of times.
ASCII
code can be used to display an equivalent
character.
String
expression is the character to be displayed.
Example:
REM Example
of STRING$ function. CLS PRINT
STRING$(5, "AVN") PRINT
STRING$(10, "=") END |
Output |
This
function is used to converts a string expression containing of digit into
numeric value. After converting, the value comes in numeric nature and can be
used for mathematical calculations.
Syntax:
VAL (string expression)
Where,
String
expression can be a string constant or string
variable.
Example:
REM Example of VAL function CLS a$ = "47AVN23" b = 15 c = b + VAL(a$) PRINT c END |
Output 62 |
STR$
This
function is used to returns the string equivalent of the ASCII value of numeric
expression.
Syntax:
STR$(numeric expression)
Where,
Numeric
expression can be any kind of number.
Example:
REM
example of STR$ function. CLS a
= 10 b$
= "Ten" a$
= STR$(a) c$
= b$ + a$ PRINT
c$ END |
Output Ten
10 |
SPACE$
This
function is used to generate a string of space of a specified number of
character length.
Syntax:
SPACE$(n)
Where,
n
is the number of characters.
Example:
REM
Example of SPACE$ function. CLS a$
= SPACE$(10) b$
= STRING$(5, "*") PRINT
b$; a$; b$ END |
Output |
OCT$
This
function returns a string representing the octal value of the numeric argument.
Syntax:
OCT$(numeric expression)
Example:
PRINT
OCT$(30) |
Output 36 |
HEX$
This
function returns a string representing the hexadecimal value of the numeric
argument.
Syntax:
HEX$(numeric expression)
Example:
PRINT
HEX$(30) |
Output 1E |
INKEY$
It
is an I/O function that returns a one or two byte string containing a character
read from the keyboard. A null string is returned if no character is waiting
there. A one character string containing the actual character read from
keyboard that reads a character from the keyboard.
Syntax:
INKEY$
Example:
REM
Example of INKEY$ function CLS PRINT
"Press any key......." DO LOOP
WHILE INKEY$ = "" END |
Output |
No comments:
Post a Comment