Substring/Top and tail: Difference between revisions
Content added Content deleted
m (add link to Delphi for pascal) |
(Added Bracmat) |
||
Line 73: | Line 73: | ||
print substr(mystring,2,length(mystring)-2) # remove both the first and last character |
print substr(mystring,2,length(mystring)-2) # remove both the first and last character |
||
}</lang> |
}</lang> |
||
=={{header|Bracmat}}== |
|||
Bracmat uses UTF-8 internally. The function <code>utf</code> fails if its argument isn't a valid UTF-8 multibyte string, but in two slightly different ways: an indefinite and a definite way. If the argument does not have the required number of bytes but otherwise seems to be ok, Bracmat's backtacking mechanism lenghtens the argument and then calls <code>utf</code> again. This is repeated until utf either succeeds or definitely fails. The code is far from efficient. |
|||
<lang bracmat>(substringUTF-8= |
|||
@( Δημοτική |
|||
: (%?a&utf$!a) ?"String with first character removed" |
|||
) |
|||
& @( Δημοτική |
|||
: ?"String with last character removed" (?z&utf$!z) |
|||
) |
|||
& @( Δημοτική |
|||
: (%?a&utf$!a) |
|||
?"String with both the first and last characters removed" |
|||
(?z&utf$!z) |
|||
) |
|||
& out |
|||
$ ("String with first character removed:" !"String with first character removed") |
|||
& out |
|||
$ ("String with last character removed:" !"String with last character removed") |
|||
& out |
|||
$ ( "String with both the first and last characters removed:" |
|||
!"String with both the first and last characters removed" |
|||
));</lang> |
|||
<lang bracmat>!substringUTF-8 |
|||
String with first character removed: ημοτική |
|||
String with last character removed: Δημοτικ |
|||
String with both the first and last characters removed: ημοτικ</lang> |
|||
If the string is known to consist of 8-byte characters, we can use a simpler method. Essential are the <code>%</code> and <code>@</code> prefixes. The <code>%</code> prefix matches 1 or more elements (bytes, in the case of string pattern matching), while <code>@</code> matches 0 or 1 elements. In combination these prefixes match 1 and only 1 byte. |
|||
<lang bracmat>(substring-8-bit= |
|||
@("8-bit string":%@ ?"String with first character removed") |
|||
& @("8-bit string":?"String with last character removed" @) |
|||
& @( "8-bit string" |
|||
: %@ ?"String with both the first and last characters removed" @ |
|||
) |
|||
& out |
|||
$ ("String with first character removed:" !"String with first character removed") |
|||
& out |
|||
$ ("String with last character removed:" !"String with last character removed") |
|||
& out |
|||
$ ( "String with both the first and last characters removed:" |
|||
!"String with both the first and last characters removed" |
|||
));</lang> |
|||
<lang bracmat>!substring-8-bit |
|||
String with first character removed: -bit string |
|||
String with last character removed: 8-bit strin |
|||
String with both the first and last characters removed: -bit strin |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |