String Theory

BlitzMax Forums/BlitzMax Tutorials/String Theory

fredborg(Posted 2004) [#1]
Here's a small introduction to some of the cool stuff you can do with strings in BlitzMax. Forget all the old basic ways of doing things (like Mid(), Left() and Right()), and see how much nicer it is in BlitzMax!
'
' String Theory
'
' A simple tutorial on
' string handling in BlitzMax
'

'
' Create a string with some random text
Local text:String = "This is just some text"

'
' Print the string to the output window
Print text

'
' Print the 3rd letter of the string to the output window
'
' Remember that the offsets start with 0 so the 3rd letter
' is located at position 2.
'
' The string is stored as an array of ascii values, so we
' need to convert it to the equivalent character to see it.
Print Chr(text[2])

'
' Print the number of letters in the string
Print text.length

'
' Print all the letters of the string one by one, and show
' the ascii value as well
'
' Remember that the last letter is located at text.length-1
' because the index starts from 0
For i = 0 To text.length-1
	Print Chr(text[i])+" = "+text[i]
Next

'
' Use the Replace method to change all the spaces to underscores
text = text.Replace(" ","_")
Print text

'
' Print the 5 left most letters of the string
'
' See how the 5 is included? That's because now we are accessing the
' string as a slice, which counts a little different from arrays.
Print text[0..5]

'
' Print the 5 right most letters of the string
Print text[text.length-5..text.length]

'
' Print the first word of the string
'
' Here we use the Find method
Print text[0..text.find("_")]

'
' Print the second word of the string
'
' Again we use the Find method
'
' Notice that we add one to the first position, so
' we don't get the underscore.
Local f1 = text.find("_")+1
Local f2 = text.find("_",f1)
Print text[f1..f2]

'
' Print each word of the string
'
' Again we use the Find method
' the Find method returns -1 if the search string
' was not found
f1 = 0
While f1<text.length
	f2 = text.find("_",f1)
	If f2<0
		f2 = text.length
	EndIf
	Print text[f1..f2]
	f1 = f2+1
Wend

'
' The End!
Print "The End!"

'
' There are many more things to try out, so take a look at the documentation for Strings
' and learn about all the great features!



ImaginaryHuman(Posted 2004) [#2]
Nice, thanks.


Bot Builder(Posted 2004) [#3]
Interesting, this tutorial inspired me to add a few more functions sincs the string object is very similar to C#'s. Only prob is the string class is totally written in C... I'll see if I can do anything :)

BTW, nice topic title :)


marksibly(Posted 2004) [#4]
Don't forget For...Until!

Local t$="Hello!"

For Local i=0 Until t.length
Print t[i]
Next


Michael Reitzenstein(Posted 2004) [#5]
Not to mention EachIn!

For Local char:Byte = EachIn "Blah"
Print Chr( char )
Next


puki(Posted 2004) [#6]
Ooh, I like this.

However, all this new stuff to learn is going to burst my brain. There is so much new stuff to learn.


ashmantle(Posted 2004) [#7]
heh.. totally what puki just said ^^


bradford6(Posted 2004) [#8]
Strings and Linked-Lists are fun...




altitudems(Posted 2005) [#9]
Ok but what If you have this:

Const MAX_LINES:int = 5
Local text:String[MAX_LINES]
text[0] = "A line of text"
'This won't work!
Print Chr(text[0])



SurreaL(Posted 2005) [#10]
You can always do this!

Const MAX_LINES:Int = 5
Local text:String[MAX_LINES]
text[0] = "A line of text"
'This works!
Print Chr(text[0][0]) 'Prints "A"
Print Chr(text[0][2]) 'Prints "l"



Russell(Posted 2005) [#11]
Mark, I'm still not clear on the difference between 'Until' and 'To' when used in this way:
Local t$="Hello!" 

For Local i=0 Until t.length 
Print t[i] 
Next 

' Should work the same as 
For Local i=0 To t.length
Print t[i]
Next
' Right?

Is it merely a choice of syntax, or is it different in some other way? The manual only mentions the Repeat\Until use of Until.

Thanks
Russell


ghislain(Posted 2005) [#12]
for..until exludes 't.length' --> "hello!"
for..next includes it --> "hello!" + 1 more character [eol?]

see language reference - program flow

ghislain