Substring/Top and tail: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
 
(6 intermediate revisions by 4 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 1,083 ⟶ 1,130:
 
=={{header|Java}}==
I solve this problem two ways. First I use substring which is relatively fast for small strings, since it simply grabs the characters within a set of given bounds. The second uses regular expressions, which have a higher overhead for such short strings, but work correctly with all Unicode code points, not just those in the Basic Multilingual Plane.
 
<syntaxhighlight lang="java">public class RM_chars {
Line 1,108 ⟶ 1,155:
room</pre>
 
Nearly all current solutions for this task fail to work correctly: the task says "The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16." The abovecode program, as most on this page,below works onlycorrectly with all Unicode codecharacters, pointswithout inusing theregular Basicexpressions Multilingual Plane. The code below, onas the otherabove hand,program works correctly with all Unicode charactersdoes.
 
<syntaxhighlight lang="java">public class SubstringTopAndTail {
Line 1,114 ⟶ 1,161:
var s = "\uD83D\uDC0Eabc\uD83D\uDC0E"; // Horse emoji, a, b, c, horse emoji: "🐎abc🐎"
 
var sizeOfFirstChar = Character.isSurrogate(s.offsetByCodePointscharAt(0,)) ? 2 : 1);
var sizeOfLastChar = Character.isSurrogate(s.codePointCountcharAt(s.length() - 2, s.length(1)) == 1 ? 2 : 1;
 
var removeFirst = s.substring(sizeOfFirstChar);
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