Substring/Top and tail: Difference between revisions
Content added Content deleted
(Added AppleScript example.) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 144: | Line 144: | ||
knight |
knight |
||
night</pre> |
night</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|AWK}} |
|||
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
|||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
|||
<lang algol68>#!/usr/local/bin/a68g --script # |
|||
STRING str="upraisers"; |
|||
printf(($gl$, |
|||
str, # remove no characters # |
|||
str[LWB str+1: ], # remove the first character # |
|||
str[ :UPB str-1], # remove the last character # |
|||
str[LWB str+1:UPB str-1], # remove both the first and last character # |
|||
str[LWB str+2: ], # remove the first 2 characters # |
|||
str[ :UPB str-2], # remove the last 2 characters # |
|||
str[LWB str+1:UPB str-2], # remove 1 before and 2 after # |
|||
str[LWB str+2:UPB str-1], # remove 2 before and one after # |
|||
str[LWB str+2:UPB str-2] # remove both the first and last 2 characters # |
|||
))</lang> |
|||
Output: |
|||
<pre> |
|||
upraisers |
|||
praisers |
|||
upraiser |
|||
praiser |
|||
raisers |
|||
upraise |
|||
praise |
|||
raiser |
|||
raise |
|||
</pre> |
|||
=={{header|Apex}}== |
=={{header|Apex}}== |
||
Line 210: | Line 242: | ||
This is some tex |
This is some tex |
||
his is some tex"</pre> |
his is some tex"</pre> |
||
=={{header|ALGOL 68}}== |
|||
{{trans|AWK}} |
|||
{{works with|ALGOL 68|Revision 1 - no extensions to language used.}} |
|||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}} |
|||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}} |
|||
<lang algol68>#!/usr/local/bin/a68g --script # |
|||
STRING str="upraisers"; |
|||
printf(($gl$, |
|||
str, # remove no characters # |
|||
str[LWB str+1: ], # remove the first character # |
|||
str[ :UPB str-1], # remove the last character # |
|||
str[LWB str+1:UPB str-1], # remove both the first and last character # |
|||
str[LWB str+2: ], # remove the first 2 characters # |
|||
str[ :UPB str-2], # remove the last 2 characters # |
|||
str[LWB str+1:UPB str-2], # remove 1 before and 2 after # |
|||
str[LWB str+2:UPB str-1], # remove 2 before and one after # |
|||
str[LWB str+2:UPB str-2] # remove both the first and last 2 characters # |
|||
))</lang> |
|||
Output: |
|||
<pre> |
|||
upraisers |
|||
praisers |
|||
upraiser |
|||
praiser |
|||
raisers |
|||
upraise |
|||
praise |
|||
raiser |
|||
raise |
|||
</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 248: | Line 248: | ||
MsgBox % SubStr(MyString, 1, StrLen(MyString)-1) |
MsgBox % SubStr(MyString, 1, StrLen(MyString)-1) |
||
MsgBox % SubStr(MyString, 2, StrLen(MyString)-2)</lang> |
MsgBox % SubStr(MyString, 2, StrLen(MyString)-2)</lang> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang awk>BEGIN { |
<lang awk>BEGIN { |
||
Line 286: | Line 287: | ||
KNIGHT |
KNIGHT |
||
NIGHT</pre> |
NIGHT</pre> |
||
=={{header|BBC BASIC}}== |
|||
<lang bbcbasic> s$ = "Rosetta Code" |
|||
PRINT MID$(s$, 2) |
|||
PRINT LEFT$(s$) |
|||
PRINT LEFT$(MID$(s$, 2))</lang> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Line 337: | Line 344: | ||
String with last character removed: 8-bit strin |
String with last character removed: 8-bit strin |
||
String with both the first and last characters removed: -bit strin</pre> |
String with both the first and last characters removed: -bit strin</pre> |
||
=={{header|BBC BASIC}}== |
|||
<lang bbcbasic> s$ = "Rosetta Code" |
|||
PRINT MID$(s$, 2) |
|||
PRINT LEFT$(s$) |
|||
PRINT LEFT$(MID$(s$, 2))</lang> |
|||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
Line 391: | Line 392: | ||
ANSI C provides little functionality for text manipulation outside of string.h. While a number of libraries for this purpose have been written, this example uses only ANSI C. |
ANSI C provides little functionality for text manipulation outside of string.h. While a number of libraries for this purpose have been written, this example uses only ANSI C. |
||
=={{header|C++}}== |
|||
<lang cpp>#include <string> |
|||
#include <iostream> |
|||
int main( ) { |
|||
std::string word( "Premier League" ) ; |
|||
std::cout << "Without first letter: " << word.substr( 1 ) << " !\n" ; |
|||
std::cout << "Without last letter: " << word.substr( 0 , word.length( ) - 1 ) << " !\n" ; |
|||
std::cout << "Without first and last letter: " << word.substr( 1 , word.length( ) - 2 ) << " !\n" ; |
|||
return 0 ; |
|||
}</lang> |
|||
Output: |
|||
<PRE>Without first letter: remier League ! |
|||
Without last letter: Premier Leagu ! |
|||
Without first and last letter: remier Leagu ! |
|||
</PRE> |
|||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
Line 429: | Line 413: | ||
tes |
tes |
||
es</pre> |
es</pre> |
||
=={{header|C++}}== |
|||
<lang cpp>#include <string> |
|||
#include <iostream> |
|||
int main( ) { |
|||
std::string word( "Premier League" ) ; |
|||
std::cout << "Without first letter: " << word.substr( 1 ) << " !\n" ; |
|||
std::cout << "Without last letter: " << word.substr( 0 , word.length( ) - 1 ) << " !\n" ; |
|||
std::cout << "Without first and last letter: " << word.substr( 1 , word.length( ) - 2 ) << " !\n" ; |
|||
return 0 ; |
|||
}</lang> |
|||
Output: |
|||
<PRE>Without first letter: remier League ! |
|||
Without last letter: Premier Leagu ! |
|||
Without first and last letter: remier Leagu ! |
|||
</PRE> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 544: | Line 545: | ||
2013-09-04 17:08:09.454 a.out[2257:507] room |
2013-09-04 17:08:09.454 a.out[2257:507] room |
||
2013-09-04 17:08:09.455 a.out[2257:507] ημοτικ</pre> |
2013-09-04 17:08:09.455 a.out[2257:507] ημοτικ</pre> |
||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x : |
ELENA 4.x : |
||
Line 617: | Line 619: | ||
puts(1, strip_last("write")) -- strip last character |
puts(1, strip_last("write")) -- strip last character |
||
puts(1, strip_both("brooms")) -- strip both first and last characters</lang> |
puts(1, strip_both("brooms")) -- strip both first and last characters</lang> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 727: | Line 728: | ||
First and last removed: ημοτικ |
First and last removed: ημοτικ |
||
</pre> |
</pre> |
||
=={{header|GW-BASIC}}== |
|||
<lang qbasic>10 A$="knight":B$="socks":C$="brooms" |
|||
20 PRINT MID$(A$,2) |
|||
30 PRINT LEFT$(B$,LEN(B$)-1) |
|||
40 PRINT MID$(C$,2,LEN(C$)-2)</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 753: | Line 748: | ||
tail: praisers |
tail: praisers |
||
top&tail: praiser</pre> |
top&tail: praiser</pre> |
||
=={{header|GW-BASIC}}== |
|||
<lang qbasic>10 A$="knight":B$="socks":C$="brooms" |
|||
20 PRINT MID$(A$,2) |
|||
30 PRINT LEFT$(B$,LEN(B$)-1) |
|||
40 PRINT MID$(C$,2,LEN(C$)-2)</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 949: | Line 950: | ||
ÜÄÖ |
ÜÄÖ |
||
Ä =={{header|Lasso}}== |
|||
Ä |
|||
<lang Lasso>local(str = 'The quick grey rhino jumped over the lazy green fox.') |
|||
// String with first character removed |
|||
string_remove(#str,-startposition=1,-endposition=1) |
|||
// > he quick grey rhino jumped over the lazy green fox. |
|||
// String with last character removed |
|||
string_remove(#str,-startposition=#str->size,-endposition=#str->size) |
|||
// > The quick grey rhino jumped over the lazy green fox |
|||
// String with both the first and last characters removed |
|||
string_remove(string_remove(#str,-startposition=#str->size,-endposition=#str->size),-startposition=1,-endposition=1) |
|||
// > he quick grey rhino jumped over the lazy green fox</lang> |
|||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
Line 969: | Line 983: | ||
30 PRINT LEFT$(b$,LEN(b$)-1) |
30 PRINT LEFT$(b$,LEN(b$)-1) |
||
40 PRINT MID$(c$,2,LEN(c$)-2)</lang> |
40 PRINT MID$(c$,2,LEN(c$)-2)</lang> |
||
=={{header|Lasso}}== |
|||
<lang Lasso>local(str = 'The quick grey rhino jumped over the lazy green fox.') |
|||
// String with first character removed |
|||
string_remove(#str,-startposition=1,-endposition=1) |
|||
// > he quick grey rhino jumped over the lazy green fox. |
|||
// String with last character removed |
|||
string_remove(#str,-startposition=#str->size,-endposition=#str->size) |
|||
// > The quick grey rhino jumped over the lazy green fox |
|||
// String with both the first and last characters removed |
|||
string_remove(string_remove(#str,-startposition=#str->size,-endposition=#str->size),-startposition=1,-endposition=1) |
|||
// > he quick grey rhino jumped over the lazy green fox</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 1,237: | Line 1,236: | ||
print $bits; # h |
print $bits; # h |
||
print $string; # ouc # See we really did chop the last letter off</lang> |
print $string; # ouc # See we really did chop the last letter off</lang> |
||
=={{header|Perl 6}}== |
|||
Perl 6 provides both functional and method forms of substr. Note that, unlike in Perl 5, offsets from the end do not use negative numbers, but instead require a function expressing the negative offset relative to the length parameter, which is supplied by the operator. The form <tt>*-1</tt> is just a simple way to write such a function. |
|||
We use musical sharps and flats to illustrate that Perl is comfortable with characters from any Unicode plane. |
|||
<lang perl6>my $s = '𝄪♯♮♭𝄫'; |
|||
print qq:to/END/; |
|||
Original: |
|||
$s |
|||
Remove first character: |
|||
{ substr($s, 1) } |
|||
{ $s.substr(1) } |
|||
Remove last character: |
|||
{ substr($s, 0, *-1) } |
|||
{ $s.substr( 0, *-1) } |
|||
{ $s.chop } |
|||
Remove first and last characters: |
|||
{ substr($s, 1, *-1) } |
|||
{ $s.substr(1, *-1) } |
|||
END</lang> |
|||
{{out}} |
|||
<pre>Original: |
|||
𝄪♯♮♭𝄫 |
|||
Remove first character: |
|||
♯♮♭𝄫 |
|||
♯♮♭𝄫 |
|||
Remove last character: |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
Remove first and last characters: |
|||
♯♮♭ |
|||
♯♮♭</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 1,478: | Line 1,435: | ||
"トリン" |
"トリン" |
||
</pre> |
</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Perl 6 provides both functional and method forms of substr. Note that, unlike in Perl 5, offsets from the end do not use negative numbers, but instead require a function expressing the negative offset relative to the length parameter, which is supplied by the operator. The form <tt>*-1</tt> is just a simple way to write such a function. |
|||
We use musical sharps and flats to illustrate that Perl is comfortable with characters from any Unicode plane. |
|||
<lang perl6>my $s = '𝄪♯♮♭𝄫'; |
|||
print qq:to/END/; |
|||
Original: |
|||
$s |
|||
Remove first character: |
|||
{ substr($s, 1) } |
|||
{ $s.substr(1) } |
|||
Remove last character: |
|||
{ substr($s, 0, *-1) } |
|||
{ $s.substr( 0, *-1) } |
|||
{ $s.chop } |
|||
Remove first and last characters: |
|||
{ substr($s, 1, *-1) } |
|||
{ $s.substr(1, *-1) } |
|||
END</lang> |
|||
{{out}} |
|||
<pre>Original: |
|||
𝄪♯♮♭𝄫 |
|||
Remove first character: |
|||
♯♮♭𝄫 |
|||
♯♮♭𝄫 |
|||
Remove last character: |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
𝄪♯♮♭ |
|||
Remove first and last characters: |
|||
♯♮♭ |
|||
♯♮♭</pre> |
|||
=={{header|Raven}}== |
=={{header|Raven}}== |