Feature Request: String.Truncate method

BlitzMax Forums/BlitzMax Programming/Feature Request: String.Truncate method

N(Posted 2005) [#1]
Simple enough, I just want a method for strings called Truncate so I don't have to manually do the slicing myself.

Just something like myString = myString.Truncate(3) would remove three characters from myString and return the new one.


JazzieB(Posted 2005) [#2]
Isn't slicing easier?

myString = myString.Truncate(3)

versus

myString = myString[..3]

???

One thing I am missing is String$(), as in a$=String("*",10). OK, so I could easily write a Function for that one.


N(Posted 2005) [#3]
myString[..3] would return the first three characters, not cut 3 characters off the end of the string.

Edit: To clarify:
http://dictionary.reference.com/search?q=truncate

Feeling stupid yet? ;)


Dreamora(Posted 2005) [#4]
then you could use

myString[..(myString.length - 3)]

or am I wrong?

( but I agree that a truncate command would be more straight and far easier to read in source )


EddieRay(Posted 2005) [#5]
Maybe it supports a notation similar to the following?

myString[..-3]


N(Posted 2005) [#6]
Eddie: No.. that works (in that it compiles and runs fine), but it doesn't truncate a string.

Basically, this is what will happen:
myString$ = "I am a pie"
myString = myString[..-3]
Print myString

Rem
myString should then equal "" (or its length with be zero)
End Rem



LeisureSuitLurie(Posted 2005) [#7]
Why not just use left() or right()?


N(Posted 2005) [#8]
Because htey're the same as slicing. You have to do Length-n in order to get what you want, if you use truncate you only have to specify how many characters you want removed.


teamonkey(Posted 2005) [#9]
Actually, negative numbers in slices (in a Python way) could be very useful.


N(Posted 2005) [#10]
Actually, negative numbers in slices (in a Python way) could be very useful.


I agree entirely there. Being able to do [-10] or [-10..myString.Length] would rock.


Beaker(Posted 2005) [#11]
http://www.blitzbasic.com/Community/posts.php?topic=43913


EddieRay(Posted 2005) [#12]
That's what I was thinking - borrow the slicing tech from Python, which is very well thought out and widely used already, rather than inventing a new syntax and sematics, etc.

One of the things I hate about C++ is evidenced in most implementations of a "string" class - there's just way too many darn methods that each do almost nothing, and together, promote very wasteful and inefficient programming, for example:

myStrVar.SuperSliceMethod(0, myStrVar.Length() - 3)

When did it become "okay" to have function calls in arg lists, much less, dereferencing class objects and calling overloaded methods + calling constructors and destructors for parent classes + blah + blah + blah. Chances are, in many string classes you see out there, Length() isn't just a simple method which returns a private integer, it's another rats nest of calls to other methods to check nationalization, characters set, blah, blah, blah. Once you start with this innocent little SuperStringClass, and then put hundreds of layers of other things on top of it that use it, you start to see that your program is spending most of it's CPU time calling constructors, destructors and methods of the innocent little string class, under the hood.

This is how C++ (and other similar langs) entice you to program, and it's the main reason why Java sucks dish water...

DOH! I did it now!!!

I'm sure BMax has reasonable implementations of its classes and doesn't use OOP just "for the sake of OOP", but to truly make it more efficient to program, but flexible enough to allow more of the common paradigms.

Ed

"Run away... run away..."