Substring/Top and tail: Difference between revisions

→‎{{header|REXX}}: added two REXX versions.
(→‎{{header|REXX}}: added two REXX versions.)
Line 995:
 
=={{header|REXX}}==
===error prone===
This REXX version is error prone in that if the   '''z'''   string is either null (0 length) or is only one byte, then the '''left''' and '''substr''' BIFs will fail   (invalid '''length''' specified).
<lang rexx>/*REXX program to show removal of 1st/last/1st&last chars from a string.*/
z = 'abcdefghijk'
 
say ' the original string =' z
say 'string first character removed =' substr(z,2)
say 'string last character removed =' left(z,length(z)-1)
say 'string first & last character removed =' substr(z,2,length(z)-2)
/*stick a fork in it, we're done.*/</lang>
exit
/* ┌───────────────────────────────────────────────┐
│ however, the original string may be null, │
│ or of insufficient length which may cause the │
│ BIFs to fail (because of negative length). │
└───────────────────────────────────────────────┘ */
 
/*╔═════════════════════════════════════════════════════╗
say ' the original string =' z
however ║ However, the original string may be null, or exactly
say 'string first character removed =' substr(z,2)
or of insufficient ║ one byte in length which may will cause the BIFs to ║
say 'string last character removed =' left(z,max(0,length(z)-1))
║ fail because of either zero or a negative length.║
say 'string first & last character removed =' substr(z,2,max(0,length(z)-2))
╚═════════════════════════════════════════════════════╝*/</lang>
/*stick a fork in it,we're done.*/</lang>
'''output'''
<pre>
Line 1,021 ⟶ 1,017:
string first & last character removed = bcdefghij
</pre>
 
===robust version===
<lang rexx>/*REXX program to show removal of 1st/last/1st&last chars from a string.*/
z = 'abcdefghijk'
say ' the original string =' z
say 'string first character removed =' substr(z,2)
say 'string last character removed =' left(z,max(0,length(z)-1))
say 'string first & last character removed =' substr(z,2,max(0,length(z)-2))
/*stick a fork in it, we're done.*/</lang>
'''output is the same as the 1<sup>st</sup> REXX version.
 
===faster version===
This REXX version is slightly faster (for removing the 1st character from a string).
<lang rexx>/*REXX program to show removal of 1st/last/1st&last chars from a string.*/
z = 'abcdefghijk'
say ' the original string =' z
 
parse var z 2 zz
say 'string first character removed =' zz
 
say 'string last character removed =' left(z,max(0,length(z)-1))
say 'string first & last character removed =' substr(z,2,max(0,length(z)-2
/*stick a fork in it, we're done.*/</lang>
'''output is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ruby}}==