Newbie: Trim?
Posted: Fri Mar 23, 2007 5:53 am
Just snooped around on my first day with Rev and could not find a trim (string) function - what does one use?
TIA
Kai
TIA
Kai
Questions and answers about the LiveCode platform.
https://forums.livecode.com/
Code: Select all
on mouseUp
put " It's fun to be a lunatic! " into tOriginalText
put word 1 to -1 of tOriginalText into tTtrimmedText
answer tOriginalText & return & tTrimmedText
end mouseUp
Code: Select all
on mouseUp
put " It's fun to be a lunatic! " into tOriginalText
put Trim(tOriginalText) into tTtrimmedText
answer tOriginalText & return & tTrimmedText
end mouseUp
function Trim pText
return word 1 to -1 of pText
end Trim
Code: Select all
function Trim pText, pWhichSides, pWhichChars
-- first ensure the optional parameters have meaningful values
if pWhichSides is empty then put "all" into pWhichSides
if pWhichChars is empty then put space into pWhichChars
-- then trim characters from left and/or right end of the string
if pWhichSides is among the items of "all,left" then
repeat while char 1 of pText is in pWhichChars
delete char 1 of pText
end repeat
end if
if pWhichSides is among the items of "all,right" then
repeat while char -1 of pText is in pWhichChars
delete char -1 of pText
end repeat
end if
return pText
end Trim
Code: Select all
if "MyLibraryStack" is not among the lines of the stacksInUse then
start using stack "MyLibraryStack"
end if
Code: Select all
function trim input
if matchText(input, "^\s*+(\S*(?:\s+\S+)*+)\s*+$", captureGroup1) then
return captureGroup1
else
return input
end if
end trim
Code: Select all
function trimEx input, trimLeft, trimRight, trimCharsLeft, trimCharsRight, trimCharsCaseSensitive
if trimCharsCaseSensitive is empty then put True into trimCharsCaseSensitive -- because PCRE is case-sens by default
if trimCharsLeft is empty then put "\s" into trimCharsLeft
if trimCharsRight is empty then put trimCharsLeft into trimCharsRight
if trimLeft is empty then put True into trimLeft
if trimRight is empty then put True into trimRight
if not trimLeft then put "" into trimCharsLeft
if not trimRight then put "" into trimCharsRight
if trimCharsLeft is "^" then put "\" before trimCharsLeft
if trimCharsRight is "^" then put "\" before trimCharsRight
put "[" & trimCharsLeft & "]" into ccPrologue
if trimCharsRight begins with "^" then -- eg "^ab"
put "[" & character 2 to -1 of trimCharsRight & "]" into ccContent -- eg "[ab]"
put "[" & trimCharsRight & "]" into ccFiller -- eg "[^ab]"
else -- eg "ab"
put "[^" & trimCharsRight & "]" into ccContent -- eg "[^ab]"
put "[" & trimCharsRight & "]" into ccFiller -- eg "[ab]"
end if
if ccPrologue is "[]" then put "" into ccPrologue
if ccPrologue is "[^]" then put "." into ccPrologue
if ccContent is "[]" then put "" into ccContent
if ccContent is "[^]" then put "." into ccContent
if ccFiller is "[]" then put "" into ccFiller
if ccFiller is "[^]" then put "." into ccFiller
put "^(?:" & ccPrologue & ")*+((?:" & ccContent & ")*(?:(?:" & ccFiller & ")+(?:" & ccContent & ")+)*+)(?:" & ccFiller & ")*+$" into trimRegEx
if not trimCharsCaseSensitive then put "(?i)" before trimRegEx
if matchText(input, trimRegEx, captureGroup1) then
return captureGroup1
else
return input
end if
end trimEx
Code: Select all
put trimEx("_ Joe Blogs xy ", True, True, " _", " xyz")
Joe Blogs
put trimEx("0.50000", False, True, " ", "0")
0.5