Strings Question

BlitzMax Forums/BlitzMax Programming/Strings Question

SillyPutty(Posted 2005) [#1]
What does this do ?

for a string

mystring[..1] and mystring[1..]

I saw this in marks code and cant make head or tail of it.


Robert(Posted 2005) [#2]
A string is basically an array of individual characters, so using BlitzMAX's array slicing tools, you can easily change the length of the string.

mystring=mystring[..n]

Resizes the mystring variable to make it n characters long. If the string is shorter than n characters, BlitzMAX will create space for the new characters. If the string is currently longer than n characters, BlitzMAX will remove all the characters after position n.

Edit: In case you aren't familiar with string / array slicing:

result=existingStringOrArray[start..end]

Takes the contents of an array or string between elements start and end and copies them into another variable.

If you omit the start parameter, the first element is used, if you omit the last parameter, the last element is used.

So to copy a string completely:

local newStr:String=oldStr[..]


SillyPutty(Posted 2005) [#3]
ah ok, thanks so much

so I can say

give me the 2 leading characters by saying

mystring[..2] ?


Perturbatio(Posted 2005) [#4]
it should be noted that with a string you CANNOT treat it exactly like an array (although slices work the same as with arrays).

doing:
Local myString:String = "This is a test"
print myString[0]

will not print the letter T
it will in fact print 84 (which is the ascii value of "T")
to print an individual character you need to use a slice.
Local myString:String = "This is a test"
Print myString[0..1]


think of the slice as [StartIndex..Count] instead of the normal [StartIndex..EndIndex]


SillyPutty(Posted 2005) [#5]
thanks guys, this really cleared everything up !