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