Substring/Top and tail: Difference between revisions

m
→‎{{header|REXX}}: redid a boxed comment, added/changed comments and whitespace.
m (→‎{{header|REXX}}: redid a boxed comment, added/changed comments and whitespace.)
Line 1,141:
===error prone===
This REXX version is error prone in that if the string is less than two characters, then the   '''left'''   and/or   '''substr'''   BIFs will fail   (because of an invalid length specified).
<lang rexx>/*REXX program demonstrates removal of 1st/last/1st&-and-last chars characters from a string. */
@ = 'abcdefghijk'
say ' the original string =' @
say 'string first character removed =' substr(@, 2)
say 'string last character removed =' left(@, length(@) -1)
say 'string first & last character removed =' substr(@, 2, length(@) -2)
/*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 However, the original string may be null ora exactlynegative length.
╚═══════════════════════════════════════════════════════════════════════════════╝ */</lang>
║ one byte in length which will cause the BIFs to ║
║ fail because of either zero or a negative length. ║
╚═══════════════════════════════════════════════════════╝ */</lang>
'''output'''
<pre>
Line 1,164 ⟶ 1,162:
===robust version===
This REXX version correctly handles a string of zero (null) or one byte.
<lang rexx>/*REXX program demonstrates removal of 1st/last/1st&-and-last chars characters from a string. */
@ = 'abcdefghijk'
say ' the original string =' @
say 'string first character removed =' substr(@, 2)
say 'string last character removed =' left(@, max(0, length(@) -1))
say 'string first & last character removed =' substr(@, 2, max(0, length(@) -2))
exit /*stick a fork in it, we're all done. */
 
/* [↓] an easier to read version using a length variable.*/
@ = 'abcdefghijk'
L=length(@)
say ' the original string =' @
say 'string first character removed =' substr(@, 2)
say 'string last character removed =' left(@, max(0, L-1) )
say 'string first & last character removed =' substr(@, 2, max(0, L-2) )</lang>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version.
 
===faster version===
This REXX version is faster &nbsp; (uses &nbsp; '''parse''' &nbsp; instead of multiple BIFs).
<lang rexx>/*REXX program demonstrates removal of 1st/last/1st&-and-last chars characters from a string. */
@ = 'abcdefghijk'
say ' the original string =' @
Line 1,190 ⟶ 1,188:
say 'string first character removed =' z
 
m=length(@) - 1
parse var @ z +(m)
say 'string last character removed =' z
 
n=length(@) - 2
parse var @ 2 z +(n)
if n==0 then z= /*handle special case of a length of 2.*/
say 'string first & last character removed =' z /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>
/*stick a fork in it, we're all done. */</lang>
'''output''' is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ring}}==