- Get link
- X
- Other Apps
Recent - Posts
- Get link
- X
- Other Apps
Diwakar here again.
Everything we've done so far in this series worked on numbers. Marks, ages, roll numbers, dice rolls. But half of what we type into a computer isn't a number at all, it's text. Your name. Your address. The title of a file. A password. So now we turn to that side of QBasic, and look at the few functions that let you cut, measure, and pull pieces out of text.
A quick reminder before we start: in QBasic, a text value is called a string, and string variables always end with a dollar sign, like name$. If you forget the $, QBasic will think you're trying to store a number, and your program won't work the way you expect.
What's in this post
- The four functions we're covering
- Think of a word as numbered boxes
- Explore all three live (try it!)
- Example 1 (Easy): How long is this name?
- Example 2 (Medium): Getting someone's initial
- Example 3 (Harder): Finding a file's extension
- Example 4 (Hero level): Guess the mobile network
- Mistakes I've seen students make
- Practice tasks
1. The four functions we're covering
LEN(string$) — tells you how many characters are in the string.
LEFT$(string$, n) — grabs the first n characters, counting from the left.
RIGHT$(string$, n) — grabs the last n characters, counting from the right.
MID$(string$, start, length) — grabs characters from somewhere in the middle, starting at position start, for length characters.
LEFT$(string$, n) — grabs the first n characters, counting from the left.
RIGHT$(string$, n) — grabs the last n characters, counting from the right.
MID$(string$, start, length) — grabs characters from somewhere in the middle, starting at position start, for length characters.
That's all four. Nothing hidden, nothing complicated. The only thing that trips people up is remembering which one to reach for, and getting the position numbers right, so that's what the rest of this post is really about.
2. Think of a word as numbered boxes
Here's how I think about it: write any word on paper, one letter per box, and number the boxes starting from 1. "COMPUTER" becomes 8 boxes, C in box 1, all the way to R in box 8. LEFT$ just means "give me the first few boxes." RIGHT$ means "give me the last few boxes." MID$ means "point to a box somewhere in the middle, and tell me how many boxes to grab from there."
One thing to be careful about: box numbering starts at 1, not 0, which is actually easier than arrays were in the last post. No OPTION BASE trap here.
3. Explore all three live
Type any word below and move the numbers around. Watch which boxes light up for each function.
Try it: LEFT$, RIGHT$ and MID$ on your own word
Change the word, then change the numbers below it, and watch the highlighted boxes move.
n =
n =
start =
len =
4. Example 1 (Easy): How long is this name?
length.bas
CLS INPUT "Enter your name: ", name$ PRINT "Your name has"; LEN(name$); "letters"
If you type "Diwakar", QBasic counts 7 characters and prints that back to you. Simple, but LEN shows up constantly once you start checking things like password length, so it's worth knowing cold.
5. Example 2 (Medium): Getting someone's initial
initial.bas
CLS INPUT "Enter your full name: ", name$ initial$ = LEFT$(name$, 1) PRINT "Your initial is "; initial$; "."
LEFT$(name$, 1) just means "give me 1 box from the left." Type "Sapkota" and you get back "S". That's the whole function, there isn't a hidden second step.
6. Example 3 (Harder): Finding a file's extension
You already know what a file extension is even if you've never thought about it this way, ".docx", ".jpg", ".bas". RIGHT$ is how you'd pull that part out of a filename.
extension.bas
CLS INPUT "Enter a filename: ", file$ ext$ = RIGHT$(file$, 4) PRINT "The extension is "; ext$
Enter "report.docx" and this grabs the last 4 characters, which is ".docx". Try it with "photo.jpg" though, and you'll notice something: it only grabs 4 characters, so you'd get "o.jpg", not ".jpg". Extensions aren't always the same length, which is exactly why this simple version has a limit, and it's a good thing to notice yourself before I point it out in the mistakes section.
7. Example 4 (Hero level): Guess the mobile network
Here's a more fun one, close to home. Most of us can tell at a glance whether a Nepali mobile number is NTC or Ncell, just by looking at the first couple of digits. Let's get QBasic to do the same thing, combining LEFT$ with the IF statement from Post 5.
network_check.bas
CLS
INPUT "Enter a 10-digit mobile number: ", num$
prefix$ = LEFT$(num$, 3)
IF prefix$ = "984" OR prefix$ = "985" OR prefix$ = "986" THEN
PRINT "This looks like an NTC number"
ELSEIF prefix$ = "980" OR prefix$ = "981" OR prefix$ = "982" THEN
PRINT "This looks like an Ncell number"
ELSE
PRINT "Not sure which network this is"
END IF
A fair warning here, I kept the prefix list short on purpose, just to keep the example readable. Real networks have more prefixes than this and they do change over time, so don't treat this as a complete lookup table, treat it as practice for how LEFT$ and IF work together. That combination, pulling out a piece of text and then deciding something based on it, is something you'll use again and again.
8. Mistakes I've seen students make
- Forgetting the $ on a string variable or function.
nameandname$are not the same thing to QBasic. - Mixing up the order of MID$'s arguments. It's always
MID$(string$, start, length), in that order, not length first. - Assuming RIGHT$ or LEFT$ is smart enough to find a symbol on its own. As you saw in Example 3, RIGHT$(file$, 4) doesn't know where the dot is, it just counts characters from the end. It has no idea what an extension even means.
- Comparing a string to a number by accident, like writing
IF age$ = 18 THENinstead ofIF age$ = "18" THEN. If a variable ends in $, whatever you compare it to needs quotes too.
9. Practice tasks
- Ask for a word and print the first and last letter separately, using LEFT$ and RIGHT$.
- Ask for someone's full name and print just their surname, assuming it's always the last 7 characters (yes, this has the same limitation as the file extension example, that's intentional).
- Use LEN and MID$ together to print the middle letter of any word with an odd number of letters.
- Challenge: using MID$ inside a FOR loop, print a word backwards, one letter at a time. This is a small taste of what a real string-reversal algorithm looks like.
That covers the basic string toolkit, LEN to measure, LEFT$ and RIGHT$ to grab from the edges, MID$ to grab from anywhere in between. None of these are difficult on their own, the skill is noticing which one fits the problem in front of you, and that only comes from actually using them a few times, not from reading about them once.
Next post, we're moving into SUB and FUNCTION procedures, which is how you stop copy-pasting the same block of code every time you need it, and instead give it a name and call it whenever you want. Before that though, go back and try the Example 3 extension program with a few different file names and see where it breaks. Finding the limits of your own code is half of learning to program.
— Diwakar
Coding Tutorials
Computer Science for Students
Computer Science Notes
Programming for Beginners
QBasic
SEE Computer
String Functions
- Get link
- X
- Other Apps
Comments