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
Printing string elements like array
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Re: Printing string elements like array
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:
Or you could use the mid() function to take a substring of length 1:
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.
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"
Code: Select all
print mid(a, 3, 1) '' "l"
mid() is safer because it won't read memory outside of the bounds of the string, but is probably a little less efficient.
Re: Printing string elements like array
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.
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
Re: Printing string elements like array
Oops!, I didn't see your post counting pine.
Re: Printing string elements like array
If speed is a thing you can also get the character like: c = strptr(a)[2]
Re: Printing string elements like array
See also this thread (I just happened to work on this thingy):
viewtopic.php?f=7&t=27203&p=255223#p255223
viewtopic.php?f=7&t=27203&p=255223#p255223
Re: Printing string elements like array
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
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