|
|
Trim Function
Added on 5/4/2005
|
This function will allow the user to trim consecutive occurances of any character or string of characters from a string. This is intended to be placed in a moviescript. It uses one function call, Trim(), with many combinations:
Trim(someString, "a") -- trims from the left and right of the string
Trim(someString, "a", #left) -- trims from the left of the string
Trim(someString, "a", #right) -- trims from the right of the string
Trim(someString, "this", #right) -- trims a string starting from the right of someString. Can use with #left as well.
-- ©2005 Josh Chunick
-- www.chunick.com/director.htm
on Trim ()
Case the paramCount of
1, 0: -- incorrect number of parameters
alert("Two parameters are required." & RETURN & "theString - a string" & RETURN & "theChar - a character or string")
exit
2: -- trim function which trims both the beginning and end of the string
theString = Trim(param(1), param(2), #left)
theString = Trim(theString, param(2), #right)
3: -- trims the left or right of the string depending on value of param(3)
theString = param(1)
theChar = param(2)
theEnd = param(3)
if not(stringP(theString)) then
alert("Please enter a string for the first parameter.")
exit
end if
if not(stringP(theChar)) then
alert("Please enter a character or string for the second parameter.")
exit
end if
theCount = theChar.char.count
Case theEnd of
#left:
if theString.char[1..theCount] = theChar then
delete char 1 to theCount of theString
theString = Trim(theString, theChar, theEnd)
end if
#right:
theLast = theString.char.count
if theString.char[theLast - (theCount - 1)..theLast] = theChar then
delete theString.char[theLast - (theCount - 1)..theLast]
theString = Trim(theString, theChar, theEnd)
end if
otherwise
alert(theEnd && "is not a valid value for the third parameter." & RETURN & "The two possible values are #left or #right.")
exit
end Case
end Case
return theString
end
|
|