Substring/Top and tail: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
 
(4 intermediate revisions by 3 users not shown)
Line 734:
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
strings$ = "EasyLangEasylang"
print substr strings$ 1 (len strings$ - 1 # Without the last character)
print substr strings$ 2 (len strings$ - 1 # Without the first character)
print substr strings$ 2 (len strings$ - 2 # Without the first and last characters)
</syntaxhighlight>
{{out}}
<pre>
Easylan
EasyLan
asylang
asyLang
asylan
asyLan
</pre>
 
=={{header|Ecstasy}}==
Generally, extracting substrings in Ecstasy is most easily performed using the slice operator, but there is also a <code>substring()</code> method:
 
<syntaxhighlight lang="ecstasy">
module Substrings {
void run(String[] args = []) {
String s = args.size > 0 ? args[0] : "hello";
@Inject Console console;
console.print(
$|Original : { s .quoted()=}
|Remove first: { s.substring(1) .quoted()=}
|Remove first: {(s.size < 1 ? "" : s[1..<s.size ]).quoted()=}
|Remove last : {(s.size < 1 ? "" : s[0..<s.size-1]).quoted()=}
|Remove both : {(s.size < 2 ? "" : s[1..<s.size-1]).quoted()=}
);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
x$ xec Substrings
Original : s .quoted()="hello"
Remove first: s.substring(1) .quoted()="ello"
Remove first: (s.size < 1 ? "" : s[1..<s.size ]).quoted()="ello"
Remove last : (s.size < 1 ? "" : s[0..<s.size-1]).quoted()="hell"
Remove both : (s.size < 2 ? "" : s[1..<s.size-1]).quoted()="ell"
x$ xec Substrings a
Original : s .quoted()="a"
Remove first: s.substring(1) .quoted()=""
Remove first: (s.size < 1 ? "" : s[1..<s.size ]).quoted()=""
Remove last : (s.size < 1 ? "" : s[0..<s.size-1]).quoted()=""
Remove both : (s.size < 2 ? "" : s[1..<s.size-1]).quoted()=""
x$ xec Substrings ab
Original : s .quoted()="ab"
Remove first: s.substring(1) .quoted()="b"
Remove first: (s.size < 1 ? "" : s[1..<s.size ]).quoted()="b"
Remove last : (s.size < 1 ? "" : s[0..<s.size-1]).quoted()="a"
Remove both : (s.size < 2 ? "" : s[1..<s.size-1]).quoted()=""
x$ xec Substrings abc
Original : s .quoted()="abc"
Remove first: s.substring(1) .quoted()="bc"
Remove first: (s.size < 1 ? "" : s[1..<s.size ]).quoted()="bc"
Remove last : (s.size < 1 ? "" : s[0..<s.size-1]).quoted()="ab"
Remove both : (s.size < 2 ? "" : s[1..<s.size-1]).quoted()="b"
</pre>
 
Line 2,160 ⟶ 2,207:
 
=={{header|RPL}}==
Basic RPL:
{{trans|Python}}
"Knight" 2 OVER SIZE SUB
"Socks" 1 OVER SIZE 1 - SUB
"Brooms" 2 OVER SIZE 1 - SUB
From HP-48 versions, one can also do this way:
"Knight" TAIL
"Socks" REVLIST TAIL REVLIST
"Brooms" TAIL REVLIST TAIL REVLIST
{{out}}
<pre>
Line 2,534 ⟶ 2,585:
{{libheader|Wren-str}}
As Wren's string slicing and other built-in methods generally work at the byte level, we use the above module for this task which works at the code-point level.
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
 
var a = "Beyoncé"
162

edits