How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the NetRexx 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 NetRexx 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 NetRexx programming language
Source code in the netrexx programming language
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method stripWhitespace(sstring, soption = 'BOTH') public static
wsChars = getWhitspaceCharacterString()
po1 = sstring.verify(wsChars)
if po1 = 0 then do
sstring = ''
end
else do
po2 = sstring.length - (sstring.reverse().verify(wsChars) - 1) + 1
ss = sstring
parse ss sl =(po1) sm =(po2) st
if po1 <= 1 then sl = ''
soption = soption.upper()
select
when 'BOTH'.abbrev(soption, 1) then sstring = sm
when 'LEADING'.abbrev(soption, 1) then sstring = sm || st
when 'TRAILING'.abbrev(soption, 1) then sstring = sl || sm
otherwise sstring = sm
end
end
return sstring
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Create an array containing a useful subset of unicode whitespace characters
*
* @return an array of unicode whitespace characters
* @see http://www.fileformat.info/info/unicode/category/index.htm
*/
method getWhitspaceCharacters() public static returns Rexx[]
wsChars = [ -
/* LINE SEPARATOR [Zi] */ '\u2028', /* PARAGRAPH SEPARATOR [Zp] */ '\u2029', -
/* SPACE [Zs] */ '\u0020', /* NO-BREAK SPACE [Zs] */ '\u00A0', -
/* OGHAM SPACE MARK [Zs] */ '\u1680', /* MONGOLIAN VOWEL SEPARATOR [Zs] */ '\u180E', -
/* EN QUAD [Zs] */ '\u2000', /* EM QUAD [Zs] */ '\u2001', -
/* EN SPACE [Zs] */ '\u2002', /* EM SPACE [Zs] */ '\u2003', -
/* THREE-PER-EM SPACE [Zs] */ '\u2004', /* FOUR-PER-EM SPACE [Zs] */ '\u2005', -
/* SIX-PER-EM SPACE [Zs] */ '\u2006', /* FIGURE SPACE [Zs] */ '\u2007', -
/* PUNCTUATION SPACE [Zs] */ '\u2008', /* THIN SPACE [Zs] */ '\u2009', -
/* HAIR SPACE [Zs] */ '\u200A', /* NARROW NO-BREAK SPACE [Zs] */ '\u202F', -
/* MEDIUM MATHEMATICAL SPACE [Zs] */ '\u3000', /* IDIOGRAPHIC SPACE [Zs] */ '\u205F', -
/* BACKSPACE [Cc] */ '\u0008', /* CHARACTER TABULATION, HT [Cc] */ '\u0009', -
/* LINE FEED (LF) [Cc] */ '\u000A', /* LINE TABULATION (VT) [Cc] */ '\u000B', -
/* FORM FEED (FF) [Cc] */ '\u000C', /* CARRIAGE RETURN (CR) [Cc] */ '\u000D', -
/* INFORMATION SEPARATOR FOUR [Cc] */ '\u001C', /* INFORMATION SEPARATOR THREE [Cc] */ '\u001D', -
/* INFORMATION SEPARATOR TWO [Cc] */ '\u001E', /* INFORMATION SEPARATOR ONE [Cc] */ '\u001F', -
/* NEXT LINE (NEL) [Cc] */ '\u0085', -
/* ZERO WIDTH SPACE [Cf] */ '\u200B', /* ZERO WIDTH NO-BREAK SPACE [Cf] */ '\uFEFF' -
]
return wsChars
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getWhitspaceCharacterString() public static returns Rexx
wsCharstring = ''
loop wsChar over getWhitspaceCharacters()
wsCharstring = wsCharstring || wsChar
end wsChar
return wsCharstring
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
sstrings = [ -
' \u0020 \u0009 \u000D\r \n \u2029\uFEFF1 String with white space. \t \r \n \u1680 ', -
' \t 2 String with white space. \t ', -
'3 String with white space. \t', -
' \t 4 String with white space.', -
'5 String with white space.', -
'\u0020\u0009\u2029\uFEFF\u1680\u2006', -
' ', -
'' -
]
loop sstringO over sstrings
sstringL = stripWhitespace(sstringO, 'l')
sstringT = stripWhitespace(sstringO, 't')
sstringB = stripWhitespace(sstringO)
say ' Original string ['sstringO']'
say ' strip leading ['sstringL']'
say ' strip trailing ['sstringT']'
say ' strip both ['sstringB']'
say
end sstringO
return
You may also check:How to resolve the algorithm Date format step by step in the COBOL programming language
You may also check:How to resolve the algorithm Stable marriage problem step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm First power of 2 that has leading decimal digits of 12 step by step in the Rust programming language
You may also check:How to resolve the algorithm Sort an array of composite structures step by step in the Fortran programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the REXX programming language