Remove tabs and empty lines

Forums for specific tips, techniques and example code
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Remove tabs and empty lines

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:55 pm

by Scott >> Wed, 1 Mar 2006 11:44:22 GMT

It possible trimBlanks use remove tabs and empty lines.

Is so, please advise the technique?

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Remove tabs and empty lines

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:55 pm

by Murray Symon >> Wed, 1 Mar 2006 20:21:14 GMT

No, you could use a simple foreach or while loop. I suggest you check out String::scanWhile() & String::scanUntil(), they may be of help.

--
Murray.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Remove tabs and empty lines

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:55 pm

by Darrell Duniam >> Wed, 1 Mar 2006 20:30:34 GMT

Try something like the attached method we have on the String primitive.

regards,
darrell.
Attachments
4408_1.mth
(1.68 KiB) Downloaded 206 times
Last edited by ConvertFromOldNGs on Fri Aug 07, 2009 4:14 pm, edited 1 time in total.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Remove tabs and empty lines

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:55 pm

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;

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Remove tabs and empty lines

Postby ConvertFromOldNGs » Fri Aug 07, 2009 2:55 pm

by Scott >> Wed, 8 Mar 2006 9:42:10 GMT

I decide this method.

Thank your help I work it well.


Return to “Tips and Techniques”

Who is online

Users browsing this forum: No registered users and 10 guests