RosettaCodeData/Task/Strip-whitespace-from-a-string-Top-and-tail/FutureBasic/strip-whitespace-from-a-string-top-and-tail.basic
2023-07-01 13:44:08 -04:00

34 lines
884 B
Text

window 1
CFCharacterSetRef set, invSet
CFStringRef s, s1, s2, s3
NSUInteger index
CFRange range
set = fn CharacterSetWhitespaceAndNewlineSet
invSet = fn CharacterSetInvertedSet( set )
text ,,,,, 200
s = @" a string "// 5 leading spaces, 8 trailing spaces
print s, len(s)@" chars"
// left trim
index = 0
range = fn StringRangeOfCharacterFromSet( s, invSet )
if ( range.location != NSNotFound ) then index = range.location
s1 = fn StringSubstringFromIndex( s, index )
print s1, len(s1)@" chars"
// right trim
index = len(s)
range = fn StringRangeOfCharacterFromSetWithOptions( s, invSet, NSBackwardsSearch )
if ( range.location != NSNotFound ) then index = range.location + 1
s2 = fn StringSubstringToIndex( s, index )
print s2, len(s2)@" chars"
// trim
s3 = fn StringByTrimmingCharactersInSet( s, set )
print s3, len(s3)@" chars"
HandleEvents