query on doing this algorithm

BlitzMax Forums/BlitzMax Beginners Area/query on doing this algorithm

stanrol(Posted 2010) [#1]
how to get last 3 letters of string.

[..3] won't work.


GW(Posted 2010) [#2]
Right("abcdefg",3)


Czar Flavius(Posted 2010) [#3]
Local s:String = "abcdefg"
Print s[s.Length-3..]



therevills(Posted 2010) [#4]
Why do you need to last 3 letters of a string?

Is it to get the type of a file? eg test.exe


Brucey(Posted 2010) [#5]
Local s:String = "abcdefg"
Print s[-3..]


... well, no, not really... but it would be nice if we could ;-)


William Drescher(Posted 2010) [#6]
Just so it's here for people wondering, if you want to get an extension of a filename, you can use ExtractExt. If you want to do this manually, you can do it like this:

Local fileName:String = "file.extension"
local filesExtension:String = fileName.Split(".")[1]

Print filesExtension


If what your trying to do is just get the last three letters of a string, just use Right.


Xerra(Posted 2010) [#7]
Using both methods...

'** Example to show how to get the file extension of a filename

SuperStrict

Local fileName:String = "Unlocked.mp3"
Local filesExtension:String = fileName.Split(".")[1]

'or you can use

Local filesExtension2:String = Right(fileName,3)  
Notify (filesExtension + " " + filesExtension2)




Dreamora(Posted 2010) [#8]
the straight split approach won't get you far, cause . is a valid letter in filenames.

The right way would be using mid and reading from end to start till you find the point and extract the rest. Reason is that file extension can be from 1 to infinite number of letters, I know file exts from at least 1 to 7 or 8 letters, so doing a right check will burn you.

alternatively you could use split and not use [1] but store the split in a string array and then get the last entry from it and then take that part off from the original string