newbie question: how to replace a character with nothing

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

newbie question: how to replace a character with nothing

Joseph Alotta
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.



Thank you for your help.

Joe.






Reply | Threaded
Open this post in threaded view
|

Re: newbie question: how to replace a character with nothing

Levente Uzonyi-2
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.

>
>
>
> Thank you for your help.
>
> Joe.
>
>
>
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: newbie question: how to replace a character with nothing

David T. Lewis
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


Reply | Threaded
Open this post in threaded view
|

Re: newbie question: how to replace a character with nothing

Mateusz Grotek
In reply to this post by Joseph Alotta
Joseph J Alotta pisze:
> Greetings.  I found a useful method.  Only thing is
> I don't know how to write the character code for no character.


Hi Joe,
For your information, there is a list for such newbie questions:
[hidden email], just to make things more organized.
Kind regards,
Mateusz