While using "string copyFrom: 2 to: string size" is the correct way to create a string without its first character, the Smalltalk newsgroup had some more amusing versions. {if you dig around in the image it's amazing what you find} ------------------------------------------------------------------------ Re: Deleting the first character of a string Author: David N. Smith Date: 1997/08/15 Forum: comp.lang.smalltalk INSANITY ON One of those 'crazy fields' swept over Danbury as I was reading this thread and I discovered another solution (in IBM Smalltalk at least):
Answers: 'ow is the time' After the field moved on I was ashamed. Dave PS: This is no where as bad as what happened the last time the field passed over... PPS: I live near a major interstate highway with heavy truck traffic; I think they're either moving crashed UFO parts or contaminated nuclear waste. PPPS: It's Friday. _____________________________________________ David N. Smith IBM T J Watson Research
Center, Hawthorne, NY Mailto: dnsmith@watson.ibm.com ------------------------------------------------------------------------ | number string newstring |
string := 'Now is the time'.
number := Integer new: string size neg: false.
string asByteArray keysAndValuesDo:
[[:i :v |
number digitAt: i put: v]].
number := number bitShift: -8.
newstring := string species new: number digitLength.
1 to: newstring size do:
[[:i |
newstring at: i put: (number digitAt:i) asCharacter]].
^newstring
Your's is shorter, but I couldn't resist
bitshifting left those bits... ------------------------------------------------------------------------ I think whatever is in those trucks must come from your town.
:-) ------------------------------------------------------------------------
By using the changeClassToThatOf: message we can avoid copying the string to the large integer and back to a string. However, this won't work for small strings since the bitShift will result in a SmallInteger which can't change its class. The next example doesn't even create a new string. Instead it uses a lightweight class. While it still contains the extra character, it is unnoticed by the inspectors:
I use lightweight classes for setting per
object breakpoints; just compile the method with a self halt
before sending the message to super. |