+withoutTrailingBlanks
authorClaus Gittinger <cg@exept.de>
Fri, 29 Feb 2008 11:12:29 +0100
changeset 10883 34881021d5ba
parent 10882 2f7bb692f56d
child 10884 36e93aa5e790
+withoutTrailingBlanks
CharacterArray.st
--- a/CharacterArray.st	Fri Feb 29 09:45:31 2008 +0100
+++ b/CharacterArray.st	Fri Feb 29 11:12:29 2008 +0100
@@ -1083,6 +1083,33 @@
 
 
 
+!
+
+withoutTrailingBlanks
+    "return a copy of myself without trailing spaces.
+     Notice: this does NOT remove tabs, newline or any other whitespace.
+     Returns an empty string, if the receiver consist only of spaces."
+
+    |index|
+
+    index := self size.
+    [index ~~ 0] whileTrue:[
+        (self at:index) == Character space ifFalse:[
+            ^ self copyTo:index
+        ].
+        index := index - 1
+    ].
+    ^ ''
+
+    "
+     '    foo    ' withoutTrailingBlanks
+     'foo    '     withoutTrailingBlanks
+     '    foo'     withoutTrailingBlanks
+     '       '     withoutTrailingBlanks
+     'foo'         withoutTrailingBlanks
+     ('  ' , Character tab asString , ' foo   ') withoutTrailingBlanks inspect
+     ('   foo' , Character tab asString) withoutTrailingBlanks inspect
+    "
 ! !
 
 !CharacterArray methodsFor:'Compatibility-V''Age'!
@@ -1404,6 +1431,7 @@
     ^ true
 ! !
 
+
 !CharacterArray methodsFor:'character searching'!
 
 includesMatchCharacters
@@ -5308,7 +5336,7 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.366 2008-02-04 12:27:15 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.367 2008-02-29 10:12:29 cg Exp $'
 ! !
 
 CharacterArray initialize!