[squeak-dev] newbie question: how to replace a character with nothing

David T. Lewis lewis at mail.msen.com
Fri Oct 26 03:39:52 UTC 2012


On Fri, Oct 26, 2012 at 05:26:58AM +0200, Levente Uzonyi wrote:
> On Thu, 25 Oct 2012, Joseph J Alotta wrote:
> 
> >Greetings.  I found a useful method.  Only thing is
> >I don't know how to write the character code for no character.
> >
> >'12%31%%1232' replaceAll: $% with: $.   '12.31..1232'
> >
> >'12%31%%1232' replaceAll: $% with: $* '12*31**1232'
> >
> >'12%31%%1232' replaceAll: $% with: ''  ==> error
> >'12%31%%1232' replaceAll: $% with: Nil  ==> error
> >
> >
> >I looked for a deleteAll: method but there is none for Strings.
> 
> Since the size of Strings (and Objects in general) can't be changed in 
> Squeak, and removing characters would change the size, therefore you have 
> to use a method which creates a new object instead of just modifying the 
> receiver. Here are a few possibilities:
> 
> '12%31%%1232' copyWithout: $%.
> '12%31%%1232' copyReplaceAll: '%' with: ''.
> '12%31%%1232' reject: [ :each | each = $% ].
> '12%31%%1232' select: [ :each | each ~= $% ].
> 
> 
> Levente
> 
> P.S.: While #replaceAll:with: looks useful, it's a bit dangerous, because 
> it changes the receiver. So you'll have to be more careful when you use 
> it.
> 

Also, the method finder will help you with questions like this.
Open a method finder:

	world menu -> open... -> method finder

Then enter an example of what you want, in the form of fields separated by
periods, like this:

	'12%31%%1232' . $% . '12311232'
	
Accept this in the entry pane, and the method finder will suggest the
#copyWithout: method and show you the classes in which it is implemented.

Dave



More information about the Squeak-dev mailing list