Printing string elements like array

New to FreeBASIC? Post your questions here.
Post Reply
Kouga
Posts: 2
Joined: Nov 30, 2018 4:14

Printing string elements like array

Post by Kouga »

Alright, I am having a hard time figuring this one out. I'm switching from c++ to freebasic since it is better documented and I'm having a hard time figuring out how to print out individual letters from a string as array elements.

Something like:
dim a as string
a = "Hello there"
print a(2)

I know it errors, but I can't quite find how to do it the correct way. I'm wanting to make a game that has a "person" typing back to you and this would help out a lot
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Printing string elements like array

Post by counting_pine »

Hi Kouga, welcome to the forum. :)
You're close, except string indexing uses '[]' like in C++, and it also returns the ASCII character code rather than the letter itself.
So you could use chr() to convert the code to a string:

Code: Select all

print chr(a[2]) '' "l"
Or you could use the mid() function to take a substring of length 1:

Code: Select all

print mid(a, 3, 1) '' "l"
Note that mid() starts at 1, while string indexing starts at 0.

mid() is safer because it won't read memory outside of the bounds of the string, but is probably a little less efficient.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Printing string elements like array

Post by dodicat »

a(2) would be an element of a freebasic string array.
However you can use string indexing, which would be more like a C array, except that each element holds the ascii value of a character.

Code: Select all

 
dim a as string
a = "Hello there"
print a[2]
print chr(a[2])
print chr(a[0])

'for array (zero based)

dim as long length=len(a)

dim as string b(0 to length-1)
for n as long=0 to length-1
    b(n)=chr(a[n])
next n
print
print b(2)
sleep 
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Printing string elements like array

Post by dodicat »

Oops!, I didn't see your post counting pine.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Printing string elements like array

Post by Munair »

If speed is a thing you can also get the character like: c = strptr(a)[2]
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Printing string elements like array

Post by Munair »

See also this thread (I just happened to work on this thingy):
viewtopic.php?f=7&t=27203&p=255223#p255223
Kouga
Posts: 2
Joined: Nov 30, 2018 4:14

Re: Printing string elements like array

Post by Kouga »

Thank you everyone for the response, and the welcome :). Thanks for the advice counting_pine and dodicat, and gonna read over that Munair!


EDIT:

Just wanted to add what I turned the suggestions into :), thanks again everyone

Code: Select all

dim a as string
a = "Why hello there"
dim b as integer
dim c as integer
dim d as integer
b = len(a)
print b
sleep
c = 0
d = 0
while c<b+1
    print chr(a[d]);
    c=c+1
    d=d+1
    sleep
wend
sleep
Post Reply