Strings

BlitzMax Forums/BlitzMax Beginners Area/Strings

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Lovely:

Test$ = "Blah"
Print Test$[0]

Output:
66

Test$ = "Blah"
Print Test$[1..]

Output:
lah

It's outputting different things depending on whether you ask for a single element or multiple elements!

I agree: that surprised me when I first encountered it. To me, only strings should be returned, never integers (that's what Asc() has always been for). It seems like pretty strange behavior for a single-element slice to return integers while a multi-element slice returns a string, but it's intentional - from the help for Asc:

Rem
Asc returns the unicode value of the first character of a string.
End Rem

print Asc("A") '65
Print "A"[0] '65 - equivalent index style implementation


There doesn't seem to be bounds-checking either (even with Debug Build):

Print "ABC"[0] ' gives 65 - OK
Print "ABC"[2] ' gives 67 - OK
Print "ABC"[3] ' gives 0 or 37008 (or other garbage, since it's some random chunk of memory) ??
Print "ABC"[9] ' gives 0 ??
Print "ABC"[-1]' gives 0 ??
Print "ABC"[-2]' gives 3 ??
Print "ABC"[-3]' gives 32767 ??