December 03, 2020, 08:02:00 PM

Author Topic: [bmx] Functions to trim whitespace from the left or right of a string by Zethrax [ 1+ years ago ]  (Read 472 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Functions to trim whitespace from the left or right of a string
Author : Zethrax
Posted : 1+ years ago

Description : RightTrim() and LeftTrim() functions to trim whitespace from the right or left of a string.

The built in BlizMax Trim() method removes the whitespace from both sides, which may be undesirable.


Code :
Code: BlitzMax
  1. Print ">" + RightTrim( "   abc   " ) + "<"
  2. Print ">" + LeftTrim( "   abc   " ) + "<"
  3.  
  4. End
  5.  
  6. Function RightTrim:String( value:String )
  7.         For Local pos:Int = value.length - 1 To 0 Step -1
  8.                 If Asc( value[ pos..pos + 1 ] ) > 32
  9.                         value = value[ ..pos + 1 ]
  10.                         Exit
  11.                 EndIf
  12.         Next
  13.         Return value
  14. End Function
  15.  
  16.  
  17. Function LeftTrim:String( value:String )
  18.         For Local pos:Int = 0 To value.length - 1
  19.                 If Asc( value[ pos..pos + 1 ] ) > 32
  20.                         value = value[ pos.. ]
  21.                         Exit
  22.                 EndIf
  23.         Next
  24.         Return value
  25. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal