Substring/Top and tail: Difference between revisions
Content added Content deleted
(Add Ecstasy example) |
|||
Line 745: | Line 745: | ||
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> |
</pre> |
||