Anonymous user
Substring/Top and tail: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments, added a faster version, added a version for easier reading.
m (→error prone: used zero instead of 0 (in a REXX section header comment)..) |
m (→{{header|REXX}}: added/changed whitespace and comments, added a faster version, added a version for easier reading.) |
||
Line 1,041:
=={{header|REXX}}==
===error prone===
This REXX version is error prone in that if the
<lang rexx>/*REXX program
say ' the original string ='
say 'string first character removed =' substr(
say 'string last character removed =' left(
say 'string first & last character removed =' substr(
/*stick a fork in it, we're all done. */
/* ╔═══════════════════════════════════════════════════════╗
║ However, the original string may be null or exactly ║
║ one byte in length which will cause the BIFs to ║
║ fail because of either zero or a negative length. ║
╚═══════════════════════════════════════════════════════╝ */</lang>
'''output'''
<pre>
Line 1,064:
===robust version===
This REXX version correctly handles a string of zero (null) or one byte.
<lang rexx>/*REXX program
say ' the original string =' z▼
say '
say 'string first
say 'string
/*stick a fork in it, we're done.*/</lang>▼
exit /*stick a fork in it, we're all done. */
/* [↓] an easier to read version using a length variable.*/
@ = 'abcdefghijk'
L=length(@)
say 'string first & last character removed =' substr(@,2,max(0,L-2))</lang>
'''output''' is the same as the 1<sup>st</sup> REXX version.
===faster version===
This REXX version is
<lang rexx>/*REXX program
say ' the original string ='
say 'string first character removed =' z
m=length(@)-1
▲parse var z 2 zz
parse var @ z +(m)
▲say 'string first character removed =' zz
say 'string last character removed =' z
n=length(@)-2
▲say 'string last character removed =' left(z,max(0,length(z)-1))
parse var @ 2 z +(n)
▲say 'string first & last character removed =' substr(z,2,max(0,length(z)-2
if n==0 then z= /*handle special case
say 'string first & last character removed =' z
▲ /*stick a fork in it, we're all done. */</lang>
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br>
|