Substring/Top and tail: Difference between revisions

m
No edit summary
(15 intermediate revisions by 8 users not shown)
Line 324:
brooms: "brooms"
 
print drop knight. 1 ; strip first character
print slice knight 1 (size knight)-1 ; alternate way to strip first character
 
Line 331:
print slice socks 0 (size socks)-2 ; yet another way to strip last character
 
print chop drop brooms 1 ; strip both first and last characters
print slice brooms 1 (size brooms)-2 ; alternate way to strip both first and last characters</syntaxhighlight>
 
Line 367:
9010 DEF FN L$(A$)=LEFT$(A$,LEN(A$)-1)
9020 DEF FN B$(A$)=FN L$(FN F$(A$))</syntaxhighlight>
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="qbasic">10 s$ = "Rosetta Code"
20 PRINT s$
30 PRINT MID$(s$,2)
40 PRINT LEFT$(s$,LEN(s$)-1)
50 PRINT MID$(s$,2,LEN(s$)-2) </syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">10 s$ = "Rosetta Code"
20 print s$
30 print mid$(s$,2)'strip first
40 print left$(s$,len(s$)-1)'strip last
50 print mid$(s$,2,len(s$)-2)'strip first and last</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 S$ = "Rosetta Code"
20 PRINT S$
30 PRINT MID$(S$, 2) 'strip first
40 PRINT LEFT$(S$, LEN(S$) - 1) 'strip last
50 PRINT MID$(S$, 2, LEN(S$) - 2) 'strip first and last</syntaxhighlight>
 
==={{header|QBasic}}===
Line 440 ⟶ 463:
KNIGHT
NIGHT</pre>
 
==={{header|Microsoft Small Basic}}===
When I tried using Unicode characters, it printed question marks, though ASCII works fine.
<syntaxhighlight lang="vbnet">
string = "Small Basic"
TextWindow.WriteLine(Text.GetSubTextToEnd(string, 2)) 'Without the first character
TextWindow.WriteLine(Text.GetSubText(string, 1, Text.GetLength(string) - 1)) 'Without the last character
TextWindow.WriteLine(Text.GetSubText(string, 2, Text.GetLength(string) - 2)) 'Without the first and last characters
</syntaxhighlight>
{{out}}
<pre>
mall Basic
Small Basi
mall Basi
</pre>
 
=={{header|BQN}}==
Line 668 ⟶ 706:
sock
room</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">void main() {
String word = "Premier League";
print("Without first letter: ${word.substring(1)} !");
print("Without last letter: ${word.substring(0, word.length - 1)} !");
print("Without first and last letter: ${word.substring(1, word.length - 1)} !");
}</syntaxhighlight>
{{out}}
<pre>Same as C++ entry.</pre>
 
=={{header|Delphi}}==
Line 684 ⟶ 732:
Readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
s$ = "Easylang"
print substr s$ 1 (len s$ - 1)
print substr s$ 2 (len s$ - 1)
print substr s$ 2 (len s$ - 2)
</syntaxhighlight>
{{out}}
<pre>
Easylan
asylang
asylan
</pre>
 
=={{header|Eero}}==
Line 908 ⟶ 970:
Last rune removed: Δημοτικ
First and last removed: ημοτικ
</pre>
 
=={{header|Golfscript}}==
When I tried using Unicode characters, the interpreter generated a mess, though ASCII works fine.
<syntaxhighlight lang="golfscript">
"Golfscript"(;n
"Golfscript");n
"Golfscript"(;);
</syntaxhighlight>
{{out}}
<pre>
olfscript
Golfscrip
olfscrip
</pre>
 
Line 1,007 ⟶ 1,083:
 
=={{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,032 ⟶ 1,108:
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 code below works correctly with all Unicode characters, without using regular expressions as the above program does.
=={{header|JavaScript}}==
 
<syntaxhighlight lang="java">public class SubstringTopAndTail {
public static void main( String[] args ){
var s = "\uD83D\uDC0Eabc\uD83D\uDC0E"; // Horse emoji, a, b, c, horse emoji: "🐎abc🐎"
 
var sizeOfFirstChar = Character.isSurrogate(s.charAt(0)) ? 2 : 1;
var sizeOfLastChar = Character.isSurrogate(s.charAt(s.length() - 1)) ? 2 : 1;
 
var removeFirst = s.substring(sizeOfFirstChar);
var removeLast = s.substring(0, s.length() - sizeOfLastChar);
var removeBoth = s.substring(sizeOfFirstChar, s.length() - sizeOfLastChar);
 
System.out.println(removeFirst);
System.out.println(removeLast);
System.out.println(removeBoth);
}
}</syntaxhighlight>
 
Results:
<pre>abc🐎
🐎abc
abc</pre>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">alert("knight".slice(1)); // strip first character
alert("socks".slice(0, -1)); // strip last character
alert("brooms".slice(1, -1)); // strip both first and last characters</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE
dropfirst == 1 drop;
droplast == dup size pred take.
 
"abcd" dropfirst.
"abcd" droplast.
"abcd" dropfirst droplast.</syntaxhighlight>
If a string is known to be non-empty, the <code>rest</code> operator could be used instead of <code>dropfirst</code>.
{{out}}
<pre>"bcd"
"abc"
"bc"</pre>
 
=={{header|jq}}==
Line 2,045 ⟶ 2,158:
substr(aString,2,len(aString)-2) + nl
</syntaxhighlight>
 
=={{header|RPL}}==
Basic RPL:
"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>
3: night
2: Sock
1: room
</pre>
 
=={{header|Ruby}}==
Line 2,125 ⟶ 2,254:
(define (string-top-tail s)
(string-tail (string-top s)))</syntaxhighlight>
 
=={{header|sed}}==
Remove the first character:
<syntaxhighlight lang="sed">s/.//</syntaxhighlight>
Remove the last character:
<syntaxhighlight lang="sed">s/.$//</syntaxhighlight>
Remove the first and the last character in one step (a bit more complex, to correctly handle single-character strings):
<syntaxhighlight lang="sed">s/.\(\(.*\).\)\{0,1\}/\2/</syntaxhighlight>
 
=={{header|Seed7}}==
Line 2,401 ⟶ 2,538:
{{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é"
2,052

edits