ID:195043
 
/*

Title: Trim Whitespace
Credit to: CaptFalcon33035
Contributed by: CaptFalcon33035

This little snippet can be used to trim whitespace off of the beginning and
the end of a string. It's simple enough to use--the string is passed as the
first parameter and the method is passed as the second. The method can be
one of three values--HEADING_WHITESPACE, TRAILING_WHITESPACE, or default
HEADING_WHITESPACE|TRAILING_WHITESPACE, which means it will take the
whitespace from both sides of the string.

*/



#define HEADING_WHITESPACE 1
#define TRAILING_WHITESPACE 2

proc/TrimWhitespace(var/string, var/method=HEADING_WHITESPACE|TRAILING_WHITESPACE)
if(!string || !istext(string) || !method || isnum(method)) return string
var/char = 1
var/currentChar
if(method & HEADING_WHITESPACE)
char = 1
currentChar = copytext(string, char, char+1)
while(currentChar && (currentChar <= " "))
char ++
currentChar = copytext(string, char, char+1)
string = copytext(string, char)
if(method & TRAILING_WHITESPACE)
char = lentext(string)
currentChar = copytext(string, char, char+1)
while(currentChar && (currentChar <= " "))
char --
currentChar = copytext(string, char, char+1)
string = copytext(string, 1, char+1)
return string
/*

Title: Trim White space
Credit to: Metamorphman
Contributed by: Metamorphman

This method of white space trimming uses text2ascii() to check the beginning, or ending characters in a string and
cut them off if necessary, thus trimming the whitespace off the string.

Method of use is simple, just pass a string as the
only argument in one of the following procs to have the corresponding whitespace cut off.

*/

proc/trimRight( text )
var/x = 1
for( x, x < length( text ), x++ )
if( text2ascii( text, x ) == 32 )
continue
break
return copytext( text, x )

proc/trimLeft( text )
var/x = length( text )
for( x, x > 1 , x-- )
if( text2ascii( text, x ) == 32 )
continue
break
return copytext( text, 1, x+1 )

proc/trimAll( text )
return trimRight( trimLeft( text ) )