Hello,
I just started learning smalltalk and could not find a message for inserting character into a given string, something like "insertAt:with:" etc. How it can be achieved? Regards, Dmitry |
> I just started learning smalltalk and could not find a message for
inserting > character into a given string, something like "insertAt:with:" etc. 'Hello' at:1 put:$T. But this message overwrites the character $H. You can use this: insertAt: anInteger with: aString " Insert aString into the receiver in the posistion anInteger " ^(self leftString: anInteger), aString, (self rightString: self size - anInteger) Best Regards Bruno Buzzi Brasesco PS: test this message very well because is not tested. |
In reply to this post by Dmitry Pankratov
Dmitry,
> I just started learning smalltalk and could not find a message for inserting > character into a given string, something like "insertAt:with:" etc. > > How it can be achieved? Streams are often a good way to do this kind of thing. One example would be | out | out := String writeStream. out nextPutAll:'Hello'; nextPut:$!. out contents inspect. Can you give a more detailed example of what you want to do? Have a good one, Bill -- Wilhelm K. Schwab, Ph.D. [hidden email] |
Hello Bill,
Thanks for the response, here is what I want to do: suppose I have a string 'Hllo', and my goal is to put missing 'e' between 'H' and 'l'. I cannot find appropriate message in String-related classes. "Bill Schwab" <[hidden email]> wrote in message news:acdkg6$ocrnt$[hidden email]... > Dmitry, > > > I just started learning smalltalk and could not find a message for > inserting > > character into a given string, something like "insertAt:with:" etc. > > > > How it can be achieved? > > Streams are often a good way to do this kind of thing. One example would be > > | out | > out := String writeStream. > out nextPutAll:'Hello'; nextPut:$!. > out contents inspect. > > Can you give a more detailed example of what you want to do? > > Have a good one, > > Bill > > -- > Wilhelm K. Schwab, Ph.D. > [hidden email] > > > |
Dmitry,
> suppose I have a string 'Hllo', and my goal is to put missing 'e' between > 'H' and 'l'. > I cannot find appropriate message in String-related classes. Try 'Hllo' copyReplaceAll: 'Hl' with: 'Hel' It's implemented in the SequenceableCollection class and works for all it's subclasses, which includes String. If you need to do it by the index of the missing character you could use s := 'Hllo'. index := 2. (s copyFrom: 1 to: index - 1) , 'e' , (s copyFrom: index) Regards Ian |
In reply to this post by Dmitry Pankratov
Dmitry,
> Thanks for the response, here is what I want to do: > suppose I have a string 'Hllo', and my goal is to put missing 'e' between > 'H' and 'l'. > I cannot find appropriate message in String-related classes. You can follow Ian's suggestion, or use something like this: | in out | in := 'Hllo' readStream. out := String writeStream. out nextPut:in next; nextPut:$e; nextPutAll:in upToEnd. out contents. Have a good one, Bill --- Wilhelm K. Schwab, Ph.D. [hidden email] |
In reply to this post by Dmitry Pankratov
"Dmitry Pankratov" <[hidden email]> wrote in message
news:3cea5c32$0$52035$[hidden email]... > Hello Bill, > > Thanks for the response, here is what I want to do: > suppose I have a string 'Hllo', and my goal is to put missing 'e' between > 'H' and 'l'. > I cannot find appropriate message in String-related classes. > > "Bill Schwab" <[hidden email]> wrote in message > news:acdkg6$ocrnt$[hidden email]... > > Dmitry, > > > > > I just started learning smalltalk and could not find a message for > > inserting > > > character into a given string, something like "insertAt:with:" etc. > > > > > > How it can be achieved? > > Another option: String fromString: ('Hllo' asOrderedCollection add: $e afterIndex: 1; yourself) |
In reply to this post by Dmitry Pankratov
[hidden email] (Dmitry Pankratov) wrote (abridged):
> I just started learning smalltalk and could not find a message for > inserting character into a given string, something like > "insertAt:with:" etc. Just to clarify some of the other replies: in Smalltalk we tend to create a new string instead of modifying the old one in-place. For example: |s1 s2| s1 := 'Hllo'. s2 := s1 copyReplaceAll: 'Hl' with: 'Hel'. The last line makes a new string and puts it in s2. The old string is unchanged (and still attached to s1). This can take getting used to. It often leads to simpler code, though, with fewer nasty side-effects. Dave Harris, Nottingham, UK | "Weave a circle round him thrice, [hidden email] | And close your eyes with holy dread, | For he on honey dew hath fed http://www.bhresearch.co.uk/ | And drunk the milk of Paradise." |
In reply to this post by Chris Hayes-3
Chris, Dave, Bill, Ian, Bruno - thanks for your solutions! Now I feel a
litle bit more comfortable with Smalltalk philosophy :) I have a big experience in C++, but it gave me almost nothing here...:) I found this solution most suitable for my project: > String fromString: > ('Hllo' asOrderedCollection > add: $e afterIndex: 1; > yourself) Regards, Dmitry |
Free forum by Nabble | Edit this page |