Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Don't fall for the mid$ trap
#1
Intuitively you might think this would come back as "yes" answer:

a$ = "apples pears"
If Mid$(a$, 7) = " " Then
    Print "yes"
Else
    Print "no"
End If

Sorry it's a"no", but this is a "yes"

a$ = "apples pears"
If Asc(a$, 7) = 32 Then
    Print "yes"
Else
    Print "no"
End If

If it wasn't for the Wiki mentioning ASC is faster.  I am have been stuck a bit longer.  Reality is mid$ is used to move strings into or out of strings.  Not for testing a value.  Here is another tibit.  Mother tongue QB45 says yes "mid$" is wrong usage, "asc" is the way.  But "why is there always a but", maybe because everyone has one.  QB45 only gets the first ASC character in the string.  QB64pe allows a position of your choice for the asc value.  For all my "QB" life I only used mid$ for substitute or pull character(s) in a string.  Learn something new everyday.  How much do you want to bet, I will forget this lesson.

/flame shield on

Ok come at me.
Reply
#2
One thing of note -- this is the incorrect usage of MID$ for what you want to do:

a$ = "apples pears"
If Mid$(a$, 7) = " " Then


Notice that you're not telling it how many spaces you want from that string, so what you're basically saying is:
Go to the middle of the string, start at the 7th position, and return the rest of the string.
Mid$(a$, 7) = " pears"

What you actually wanted here was this usage:

If Mid$(a$, 7, 1) = " " Then

Notice that you're now telling it how many spaces you want from that string, so what you're basically saying is:
Go to the middle of the string, start at the 7th position, and return one character from the string.

Missed parameter = missed result.  Wink




That said; use the ASC version anyway, when you ever can.  It's much faster.  String comparison and manipulation is rather slow, but we do numeric ASCII compares quite effiicently.
Reply
#3
Ok, my mistake and a lesson about mid$.  Thanks for the double hit Steve.
Reply
#4
Happy to help.  Just remember, the day we quit learning is the day we start dying.   We all do stuff like this, and some of this syntax isn't the most intuitive until you get used to seeing it and using it a lot of different times.  It's always nice to have somebody come along and explain what went wrong, what we missed, and now to fix it.   It makes it much less likely that we'll fall for the same issue again in the future.  Smile
Reply
#5
F1 is your friend. Use it often.
The noticing will continue
Reply
#6
I'd say, knowing when to checkup on your assumptions is the thing here. An unexpected result is a good time to check-in with F1 or the forum.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#7
F-1??? F-Everyone!!!!

Sorry, channeling Clippy for Halloween.

Oh, and don't forget MID$ also works with negative numbers...

Code: (Select All)
a$ = "Apple Pear"
Print Mid$(a$, -3, 7)

App

Pete Big Grin
Reply
#8
(10-16-2025, 03:42 AM)Pete Wrote:
Code: (Select All)
a$ = "Apple Pear"
Print Mid$(a$, -3, 7)

App

Pete Big Grin
Wait...whaaa???  So I thought you were screwing with us, but it does in fact work and returns 'App'.  At first glance I looked and thought that it should return 4 characters but then realized that crossing 0 was effectively a digit which makes returning 3 characters correct.  Dang son you almost got me!
Reply
#9
The only program I took advantage of this little gem, so far, involved moving a string horizontally across the screen marquee style.

Sorry you thought I was just messing with you. I thought everyone realized I'm the most serious coder in these parts!

Pete Big Grin
Reply
#10
I noticed a long time ago with Pete.  He likes to f' around A LOT.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Don't let INKEY$ byte you in the ASCII Pete 1 160 02-01-2026, 10:44 PM
Last Post: PhilOfPerth
Question using $color:32 colour names mid-program? hskakw 2 192 01-14-2026, 09:30 AM
Last Post: hskakw
  Don't look now developers, but you've got a pat on your back! Pete 7 1,256 12-15-2024, 09:59 AM
Last Post: TempodiBasic
  "Well I don't give a damn about my bad reputation." TDarcos 16 3,511 09-13-2022, 12:32 AM
Last Post: Kernelpanic
  Don't make me REPETEND myself... Pete 23 4,304 08-14-2022, 11:48 AM
Last Post: Pete

Forum Jump:


Users browsing this thread: 1 Guest(s)