trimRight: problem

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

trimRight: problem

Brad Selfridge
I have a string that has an ending period (example - 'This is a string.'). I want to trim the trailing period off of the string. I've tried using:

 'This is a string.' trimRight: [ :ea | ea = $. ]

But, the period is not trimmed. Is there a way to do this without me having to extend the String class?
Brad Selfridge
Reply | Threaded
Open this post in threaded view
|

Re: trimRight: problem

Henrik Nergaard
 'This is a string.'  trimRight: [ :c | c = $. ]   works fine for me.

The trimRight method will return a new string object without the punctuation. If you just do the expression without assigning it into a variable/method call, then it will have no effect because strings are immutable so the original string will remain unchanged.
------------
  | myString trimmedString |
  myString := 'This is a string.' .
  trimmedString := 'This is a string.'  trimRight: [ :c | c = $. ].
  1 assert: (trimmedString last = $.) not;
   assert: myString ~~ trimmedString;
   assert: myString last = $.

-----------
 
Other trim options:

'This is a string.' allButLast.
'This is a string.' reject: [ :c | c =$. ].
'This is a string.' copyReplaceAll: '.' with: ''.
'' join: ($. split: 'This is a string.' ).
'This is a string.'  onlyLetters.
'This is a string.'  withoutPeriodSuffix.

Best regards,
Henrik

-----Original Message-----
From: Pharo-users [mailto:[hidden email]] On Behalf Of Brad Selfridge
Sent: Wednesday, September 14, 2016 10:34 PM
To: [hidden email]
Subject: [Pharo-users] trimRight: problem

I have a string that has an ending period (example - 'This is a string.'). I want to trim the trailing period off of the string. I've tried using:

 'This is a string.' trimRight: [ :ea | ea = $. ]

But, the period is not trimmed. Is there a way to do this without me having to extend the String class?



-----
Brad Selfridge
--
View this message in context: http://forum.world.st/trimRight-problem-tp4915603.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.