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!
|