by
Patwos >> Thu, 2 Mar 2006 1:29:53 GMT
I'll assume that what you want to achieve is the removal of leading and trailing white space characters, such as Space, Tab, Cr, Lf etc. (Mentioning trimBlanks in your question is suggestive that this is what you are trying to achieve rather than the removal of all these characters from a given string.)
You may find the following a useful method to add to your String primitive. It uses the Jade supplied String::scanWhile and String::reverse methods, which execute as external methods, to help improve its efficiency when dealing with large strings.
If you require more flexibility, you could pass the whiteSpace characters in as a parameter. I've used a local constant in this sample code.
Hope that helps,
Pat.
__________________________________________________________
Code: Select all
trimWhiteSpace() : String;
constants
WhiteSpaceCharacters : String = Cr & Lf & Tab & " " ;
vars
startPos : Integer ;
endPos : Integer ;
str : String ;
begin
if self = null then
return null ;
endif;
// Find the first non-white-space character
startPos := 1 ;
self.scanWhile( WhiteSpaceCharacters, startPos );
// Does string contain nothing but white space? If so, return null
if startPos = 0 then
return null ;
endif;
// Now find the last non-white-space character, reversing the string so we can use scanWhile again for efficiency
str := self.reverse;
endPos := 1 ;
str.scanWhile( WhiteSpaceCharacters, endPos );
endPos := self.length - endPos + 1 ;
// Create the final string trimmed of leading and trailing whitespace
str := self[startPos : endPos - startPos + 1 ];
return str ;
end;