How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the FutureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the FutureBasic programming language

Table of Contents

Problem Statement

Demonstrate how to strip leading and trailing whitespace from a string. The solution should demonstrate how to achieve the following three results:

For the purposes of this task whitespace includes non printable characters such as the space character, the tab character, and other such characters that have no corresponding graphical representation.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the FutureBasic programming language

Source code in the futurebasic programming language

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

  

You may also check:How to resolve the algorithm Infinity step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Decorate-sort-undecorate idiom step by step in the jq programming language
You may also check:How to resolve the algorithm Create a two-dimensional array at runtime step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Emacs Lisp programming language