Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Substring-Top-and-tail/00-META.yaml
Normal file
4
Task/Substring-Top-and-tail/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
from: http://rosettacode.org/wiki/Substring/Top_and_tail
|
||||
19
Task/Substring-Top-and-tail/00-TASK.txt
Normal file
19
Task/Substring-Top-and-tail/00-TASK.txt
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
The task is to demonstrate how to remove the first and last characters from a string.
|
||||
|
||||
The solution should demonstrate how to obtain the following results:
|
||||
|
||||
* String with first character removed
|
||||
* String with last character removed
|
||||
* String with both the first and last characters removed
|
||||
|
||||
<br>
|
||||
If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point, whether in the Basic Multilingual Plane or above it.
|
||||
|
||||
The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16.
|
||||
|
||||
Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.
|
||||
|
||||
|
||||
{{Template:Strings}}
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
print(‘knight’[1..])
|
||||
print(‘socks’[0 .< (len)-1])
|
||||
print(‘brooms’[1 .< (len)-1])
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
* Substring/Top and tail 04/03/2017
|
||||
SUBSTRTT CSECT
|
||||
USING SUBSTRTT,R13 base register
|
||||
B 72(R15) skip savearea
|
||||
DC 17F'0' savearea
|
||||
STM R14,R12,12(R13) save previous context
|
||||
ST R13,4(R15) link backward
|
||||
ST R15,8(R13) link forward
|
||||
LR R13,R15 set addressability
|
||||
*
|
||||
XPRNT S8,L'S8 print s8
|
||||
MVC S7,S8+1 s7=substr(s8,2,7)
|
||||
XPRNT S7,L'S7 print s7
|
||||
MVC S7,S8 s7=substr(s8,1,7)
|
||||
XPRNT S7,L'S7 print s7
|
||||
MVC S6,S8+1 s6=substr(s8,2,6)
|
||||
XPRNT S6,L'S6 print s6
|
||||
*
|
||||
L R13,4(0,R13) epilog
|
||||
LM R14,R12,12(R13) restore previous context
|
||||
XR R15,R15 rc=0
|
||||
BR R14 exit
|
||||
S8 DC CL8'12345678'
|
||||
S7 DS CL7
|
||||
S6 DS CL6
|
||||
YREGS
|
||||
END SUBSTRTT
|
||||
15
Task/Substring-Top-and-tail/ACL2/substring-top-and-tail.acl2
Normal file
15
Task/Substring-Top-and-tail/ACL2/substring-top-and-tail.acl2
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(defun str-rest (str)
|
||||
(coerce (rest (coerce str 'list)) 'string))
|
||||
|
||||
(defun rdc (xs)
|
||||
(if (endp (rest xs))
|
||||
nil
|
||||
(cons (first xs)
|
||||
(rdc (rest xs)))))
|
||||
|
||||
(defun str-rdc (str)
|
||||
(coerce (rdc (coerce str 'list)) 'string))
|
||||
|
||||
(str-rdc "string")
|
||||
(str-rest "string")
|
||||
(str-rest (str-rdc "string"))
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#!/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 #
|
||||
))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
BEGIN {
|
||||
mystring="knights"
|
||||
print substr(mystring,2) # remove the first letter
|
||||
print substr(mystring,1,length(mystring)-1) # remove the last character
|
||||
print substr(mystring,2,length(mystring)-2) # remove both the first and last character
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
PROC Main()
|
||||
CHAR ARRAY text="qwertyuiop"
|
||||
CHAR ARRAY res(20)
|
||||
BYTE n,m
|
||||
|
||||
PrintF("Original string:%E ""%S""%E%E",text)
|
||||
|
||||
SCopyS(res,text,2,text(0))
|
||||
PrintF("String without the top:%E ""%S""%E%E",res)
|
||||
|
||||
SCopyS(res,text,1,text(0)-1)
|
||||
PrintF("String without the tail:%E ""%S""%E%E",res)
|
||||
|
||||
SCopyS(res,text,2,text(0)-1)
|
||||
PrintF("String without the top and the tail:%E ""%S""%E%E",res)
|
||||
RETURN
|
||||
11
Task/Substring-Top-and-tail/Ada/substring-top-and-tail-1.ada
Normal file
11
Task/Substring-Top-and-tail/Ada/substring-top-and-tail-1.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Remove_Characters is
|
||||
S: String := "upraisers";
|
||||
use Ada.Text_IO;
|
||||
begin
|
||||
Put_Line("Full String: """ & S & """");
|
||||
Put_Line("Without_First: """ & S(S'First+1 .. S'Last) & """");
|
||||
Put_Line("Without_Last: """ & S(S'First .. S'Last-1) & """");
|
||||
Put_Line("Without_Both: """ & S(S'First+1 .. S'Last-1) & """");
|
||||
end Remove_Characters;
|
||||
33
Task/Substring-Top-and-tail/Ada/substring-top-and-tail-2.ada
Normal file
33
Task/Substring-Top-and-tail/Ada/substring-top-and-tail-2.ada
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
with Ada.Text_IO;
|
||||
with Ada.Strings.UTF_Encoding.Wide_Strings;
|
||||
|
||||
procedure Remove_Characters
|
||||
is
|
||||
use Ada.Text_IO;
|
||||
use Ada.Strings.UTF_Encoding;
|
||||
use Ada.Strings.UTF_Encoding.Wide_Strings;
|
||||
|
||||
S : String := "upraisers";
|
||||
U : Wide_String := Decode (UTF_8_String'(S));
|
||||
|
||||
function To_String (X : Wide_String)return String
|
||||
is
|
||||
begin
|
||||
return String (UTF_8_String'(Encode (X)));
|
||||
end To_String;
|
||||
|
||||
begin
|
||||
Put_Line
|
||||
(To_String
|
||||
("Full String: """ & U & """"));
|
||||
Put_Line
|
||||
(To_String
|
||||
("Without_First: """ & U (U'First + 1 .. U'Last) & """"));
|
||||
Put_Line
|
||||
(To_String
|
||||
("Without_Last: """ & U (U'First .. U'Last - 1) & """"));
|
||||
Put_Line
|
||||
(To_String
|
||||
("Without_Both: """ & U (U'First + 1 .. U'Last - 1) & """"));
|
||||
|
||||
end Remove_Characters;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
o_text(delete("knights", 0));
|
||||
o_newline();
|
||||
o_text(delete("knights", -1));
|
||||
o_newline();
|
||||
o_text(delete(delete("knights", 0), -1));
|
||||
o_newline();
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
#include <hopper.h>
|
||||
#proto showmessage(_X_)
|
||||
main:
|
||||
s="message", t=s
|
||||
++s, _show message(s)
|
||||
s=t
|
||||
--s, _show message(s)
|
||||
++s, _show message(s)
|
||||
{0}return
|
||||
|
||||
.locals
|
||||
showmessage(_S_)
|
||||
{_S_,"\n"}print
|
||||
back
|
||||
16
Task/Substring-Top-and-tail/Apex/substring-top-and-tail.apex
Normal file
16
Task/Substring-Top-and-tail/Apex/substring-top-and-tail.apex
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
String strOrig = 'brooms';
|
||||
String str1 = strOrig.substring(1, strOrig.length());
|
||||
system.debug(str1);
|
||||
String str2 = strOrig.substring(0, strOrig.length()-1);
|
||||
system.debug(str2);
|
||||
String str3 = strOrig.substring(1, strOrig.length()-1);
|
||||
system.debug(str3);
|
||||
|
||||
// Regular Expressions approach
|
||||
String strOrig = 'brooms';
|
||||
String str1 = strOrig.replaceAll( '^.', '' );
|
||||
system.debug(str1);
|
||||
String str2 = strOrig.replaceAll( '.$', '' ) ;
|
||||
system.debug(str2);
|
||||
String str3 = strOrig.replaceAll( '^.|.$', '' );
|
||||
system.debug(str3);
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
set aString to "This is some text"
|
||||
|
||||
set stringLength to (count aString) -- The number of characters in the text.
|
||||
|
||||
-- AppleScript indices are 1-based. Ranges can be specified in several different ways.
|
||||
if (stringLength > 1) then
|
||||
set substring1 to text 2 thru stringLength of aString
|
||||
-- set substring1 to text 2 thru -1 of aString
|
||||
-- set substring1 to text 2 thru end of aString
|
||||
-- set substring1 to text from character 2 to character stringLength of aString
|
||||
-- set substring1 to aString's text from 2 to -1
|
||||
-- Some combination of the above.
|
||||
else
|
||||
set substring1 to ""
|
||||
end if
|
||||
|
||||
if (stringLength > 1) then
|
||||
set substring2 to text 1 thru -2 of aString
|
||||
else
|
||||
set substring2 to ""
|
||||
end if
|
||||
|
||||
if (stringLength > 2) then
|
||||
set substring3 to text 2 thru -2 of aString
|
||||
else
|
||||
set substring3 to ""
|
||||
end if
|
||||
|
||||
return substring1 & linefeed & substring2 & linefeed & substring3
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
knight: "knight"
|
||||
socks: "socks"
|
||||
brooms: "brooms"
|
||||
|
||||
print drop knight 1 ; strip first character
|
||||
print slice knight 1 (size knight)-1 ; alternate way to strip first character
|
||||
|
||||
print chop socks ; strip last character
|
||||
print take socks (size socks)-1 ; alternate way to strip last character
|
||||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
myString := "knights"
|
||||
MsgBox % SubStr(MyString, 2)
|
||||
MsgBox % SubStr(MyString, 1, StrLen(MyString)-1)
|
||||
MsgBox % SubStr(MyString, 2, StrLen(MyString)-2)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
10 PRINT FN F$("KNIGHTS"): REM STRIP THE FIRST LETTER
|
||||
20 PRINT FN L$("SOCKS"): REM STRIP THE LAST LETTER
|
||||
30 PRINT FN B$("BROOMS"): REM STRIP BOTH THE FIRST AND LAST LETTER
|
||||
100 END
|
||||
|
||||
9000 DEF FN F$(A$)=RIGHT$(A$,LEN(A$)-1)
|
||||
9010 DEF FN L$(A$)=LEFT$(A$,LEN(A$)-1)
|
||||
9020 DEF FN B$(A$)=FN L$(FN F$(A$))
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
s$ = "Rosetta Code"
|
||||
|
||||
print s$
|
||||
PRINT MID$(s$, 2) 'strip first
|
||||
PRINT LEFT$(s$, LEN(s$) - 1) 'strip last
|
||||
PRINT MID$(s$, 2, LEN(s$) - 2) 'strip first and last
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
s$ = "Rosetta Code"
|
||||
PRINT MID$(s$, 2)
|
||||
PRINT LEFT$(s$)
|
||||
PRINT LEFT$(MID$(s$, 2))
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
str ← "substring"
|
||||
"substring"
|
||||
1↓str
|
||||
"ubstring"
|
||||
¯1↓str
|
||||
"substrin"
|
||||
1↓¯1↓str
|
||||
"ubstrin"
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
(substringUTF-8=
|
||||
@( Δημοτική
|
||||
: (%?a&utf$!a) ?"String with first character removed"
|
||||
)
|
||||
& @( Δημοτική
|
||||
: ?"String with last character removed" (?z&utf$!z)
|
||||
)
|
||||
& @( Δημοτική
|
||||
: (%?a&utf$!a)
|
||||
?"String with both the first and last characters removed"
|
||||
(?z&utf$!z)
|
||||
)
|
||||
& out
|
||||
$ ("String with first character removed:" !"String with first character removed")
|
||||
& out
|
||||
$ ("String with last character removed:" !"String with last character removed")
|
||||
& out
|
||||
$ ( "String with both the first and last characters removed:"
|
||||
!"String with both the first and last characters removed"
|
||||
));
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
(substring-8-bit=
|
||||
@("8-bit string":%@ ?"String with first character removed")
|
||||
& @("8-bit string":?"String with last character removed" @)
|
||||
& @( "8-bit string"
|
||||
: %@ ?"String with both the first and last characters removed" @
|
||||
)
|
||||
& out
|
||||
$ ("String with first character removed:" !"String with first character removed")
|
||||
& out
|
||||
$ ("String with last character removed:" !"String with last character removed")
|
||||
& out
|
||||
$ ( "String with both the first and last characters removed:"
|
||||
!"String with both the first and last characters removed"
|
||||
));
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
blsq ) "RosettaCode"[-
|
||||
"osettaCode"
|
||||
blsq ) "RosettaCode"-]
|
||||
'R
|
||||
blsq ) "RosettaCode"~]
|
||||
"RosettaCod"
|
||||
blsq ) "RosettaCode"[~
|
||||
'e
|
||||
blsq ) "RosettaCode"~-
|
||||
"osettaCod"
|
||||
10
Task/Substring-Top-and-tail/C++/substring-top-and-tail.cpp
Normal file
10
Task/Substring-Top-and-tail/C++/substring-top-and-tail.cpp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#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 ;
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string testString = "test";
|
||||
Console.WriteLine(testString.Substring(1));
|
||||
Console.WriteLine(testString.Substring(0, testString.Length - 1));
|
||||
Console.WriteLine(testString.Substring(1, testString.Length - 2));
|
||||
}
|
||||
}
|
||||
25
Task/Substring-Top-and-tail/C/substring-top-and-tail.c
Normal file
25
Task/Substring-Top-and-tail/C/substring-top-and-tail.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main( int argc, char ** argv ){
|
||||
const char * str_a = "knight";
|
||||
const char * str_b = "socks";
|
||||
const char * str_c = "brooms";
|
||||
|
||||
char * new_a = malloc( strlen( str_a ) - 1 );
|
||||
char * new_b = malloc( strlen( str_b ) - 1 );
|
||||
char * new_c = malloc( strlen( str_c ) - 2 );
|
||||
|
||||
strcpy( new_a, str_a + 1 );
|
||||
strncpy( new_b, str_b, strlen( str_b ) - 1 );
|
||||
strncpy( new_c, str_c + 1, strlen( str_c ) - 2 );
|
||||
|
||||
printf( "%s\n%s\n%s\n", new_a, new_b, new_c );
|
||||
|
||||
free( new_a );
|
||||
free( new_b );
|
||||
free( new_c );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
identification division.
|
||||
program-id. toptail.
|
||||
|
||||
data division.
|
||||
working-storage section.
|
||||
01 data-field.
|
||||
05 value "[this is a test]".
|
||||
|
||||
procedure division.
|
||||
sample-main.
|
||||
display data-field
|
||||
*> Using reference modification, which is (start-position:length)
|
||||
display data-field(2:)
|
||||
display data-field(1:length of data-field - 1)
|
||||
display data-field(2:length of data-field - 2)
|
||||
goback.
|
||||
end program toptail.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
; using substring:
|
||||
user=> (subs "knight" 1)
|
||||
"night"
|
||||
user=> (subs "socks" 0 4)
|
||||
"sock"
|
||||
user=> (.substring "brooms" 1 5)
|
||||
"room"
|
||||
|
||||
; using rest and drop-last:
|
||||
user=> (apply str (rest "knight"))
|
||||
"night"
|
||||
user=> (apply str (drop-last "socks"))
|
||||
"sock"
|
||||
user=> (apply str (rest (drop-last "brooms")))
|
||||
"room"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> (defvar *str* "∀Ꮺ✤Л◒")
|
||||
*STR*
|
||||
> (subseq *str* 1) ; remove first character
|
||||
"Ꮺ✤Л◒"
|
||||
> (subseq *str* 0 (1- (length *str*))) ; remove last character
|
||||
"∀Ꮺ✤Л"
|
||||
> (subseq *str* 1 (1- (length *str*))) ; remove first and last character
|
||||
"Ꮺ✤Л"
|
||||
12
Task/Substring-Top-and-tail/D/substring-top-and-tail.d
Normal file
12
Task/Substring-Top-and-tail/D/substring-top-and-tail.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
// strip first character
|
||||
writeln("knight"[1 .. $]);
|
||||
|
||||
// strip last character
|
||||
writeln("socks"[0 .. $ - 1]);
|
||||
|
||||
// strip both first and last characters
|
||||
writeln("brooms"[1 .. $ - 1]);
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
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)} !");
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
program TopAndTail;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
const
|
||||
TEST_STRING = '1234567890';
|
||||
begin
|
||||
Writeln(TEST_STRING); // full string
|
||||
Writeln(Copy(TEST_STRING, 2, Length(TEST_STRING))); // first character removed
|
||||
Writeln(Copy(TEST_STRING, 1, Length(TEST_STRING) - 1)); // last character removed
|
||||
Writeln(Copy(TEST_STRING, 2, Length(TEST_STRING) - 2)); // first and last characters removed
|
||||
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
string$ = "EasyLang"
|
||||
print substr string$ 1 len string$ - 1 # Without the last character
|
||||
print substr string$ 2 len string$ - 1 # Without the first character
|
||||
print substr string$ 2 len string$ - 2 # Without the first and last characters
|
||||
18
Task/Substring-Top-and-tail/Eero/substring-top-and-tail.eero
Normal file
18
Task/Substring-Top-and-tail/Eero/substring-top-and-tail.eero
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
int main()
|
||||
autoreleasepool
|
||||
|
||||
s := 'knight'
|
||||
Log( '%@', s[1 .. s.length-1] ) // strip first character
|
||||
|
||||
s = 'socks'
|
||||
Log( '%@', s[0 .. s.length-2] ) // strip last character
|
||||
|
||||
s = 'brooms'
|
||||
Log( '%@', s[1 .. s.length-2] ) // strip both first and last characters
|
||||
|
||||
s = 'Δημοτική'
|
||||
Log( '%@', s[1 .. s.length-2] ) // strip both first and last characters
|
||||
|
||||
return 0
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
var testString := "test";
|
||||
|
||||
console.printLine(testString.Substring(1));
|
||||
console.printLine(testString.Substring(0, testString.Length - 1));
|
||||
console.printLine(testString.Substring(1, testString.Length - 2))
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
iex(1)> str = "abcdefg"
|
||||
"abcdefg"
|
||||
iex(2)> String.slice(str, 1..-1)
|
||||
"bcdefg"
|
||||
iex(3)> String.slice(str, 0..-2)
|
||||
"abcdef"
|
||||
iex(4)> String.slice(str, 1..-2)
|
||||
"bcdef"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(let ((string "top and tail"))
|
||||
(substring string 1) ;=> "op and tail"
|
||||
(substring string 0 (1- (length string))) ;=> "top and tai"
|
||||
(substring string 1 (1- (length string)))) ;=> "op and tai"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
1> Str = "Hello".
|
||||
"Hello"
|
||||
2> string:sub_string(Str, 2). % To strip the string from the right by 1
|
||||
"ello"
|
||||
3> string:sub_string(Str, 1, length(Str)-1). % To strip the string from the left by 1
|
||||
"Hell"
|
||||
4> string:sub_string(Str, 2, length(Str)-1). % To strip the string from both sides by 1
|
||||
"ell"
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
function strip_first(sequence s)
|
||||
return s[2..$]
|
||||
end function
|
||||
|
||||
function strip_last(sequence s)
|
||||
return s[1..$-1]
|
||||
end function
|
||||
|
||||
function strip_both(sequence s)
|
||||
return s[2..$-1]
|
||||
end function
|
||||
|
||||
puts(1, strip_first("knight")) -- strip first character
|
||||
puts(1, strip_last("write")) -- strip last character
|
||||
puts(1, strip_both("brooms")) -- strip both first and last characters
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
[<EntryPoint>]
|
||||
let main args =
|
||||
let s = "一二三四五六七八九十"
|
||||
printfn "%A" (s.Substring(1))
|
||||
printfn "%A" (s.Substring(0, s.Length - 1))
|
||||
printfn "%A" (s.Substring(1, s.Length - 2))
|
||||
0
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
USING: io kernel sequences ;
|
||||
"Rosetta code" [ rest ] [ but-last ] [ rest but-last ] tri
|
||||
[ print ] tri@
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
: hello ( -- c-addr u )
|
||||
s" Hello" ;
|
||||
|
||||
hello 1 /string type \ => ello
|
||||
|
||||
hello 1- type \ => hell
|
||||
|
||||
hello 1 /string 1- type \ => ell
|
||||
11
Task/Substring-Top-and-tail/Fortran/substring-top-and-tail.f
Normal file
11
Task/Substring-Top-and-tail/Fortran/substring-top-and-tail.f
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
program substring
|
||||
|
||||
character(len=5) :: string
|
||||
string = "Hello"
|
||||
|
||||
write (*,*) string
|
||||
write (*,*) string(2:)
|
||||
write (*,*) string( :len(string)-1)
|
||||
write (*,*) string(2:len(string)-1)
|
||||
|
||||
end program substring
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Dim s As String = "panda"
|
||||
Dim s1 As String = Mid(s, 2)
|
||||
Dim s2 As String = Left(s, Len(s) - 1)
|
||||
Dim s3 As String = Mid(s, 2, Len(s) - 2)
|
||||
Print s
|
||||
Print s1
|
||||
Print s2
|
||||
Print s3
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
void local fn DoIt
|
||||
CFStringRef s = @"knights"
|
||||
print s
|
||||
CFStringRef s1 = mid(s, 1, len(s) - 1)
|
||||
print s1
|
||||
CFStringRef s2 = left(s, len(s) - 1)
|
||||
print s2
|
||||
CFStringRef s3 = mid(s, 1, len(s) - 2)
|
||||
print s3
|
||||
end fn
|
||||
|
||||
fn DoIt
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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)
|
||||
23
Task/Substring-Top-and-tail/Go/substring-top-and-tail.go
Normal file
23
Task/Substring-Top-and-tail/Go/substring-top-and-tail.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// ASCII contents: Interpreting "characters" as bytes.
|
||||
s := "ASCII"
|
||||
fmt.Println("String: ", s)
|
||||
fmt.Println("First byte removed: ", s[1:])
|
||||
fmt.Println("Last byte removed: ", s[:len(s)-1])
|
||||
fmt.Println("First and last removed:", s[1:len(s)-1])
|
||||
// UTF-8 contents: "Characters" as runes (unicode code points)
|
||||
u := "Δημοτική"
|
||||
fmt.Println("String: ", u)
|
||||
_, sizeFirst := utf8.DecodeRuneInString(u)
|
||||
fmt.Println("First rune removed: ", u[sizeFirst:])
|
||||
_, sizeLast := utf8.DecodeLastRuneInString(u)
|
||||
fmt.Println("Last rune removed: ", u[:len(u)-sizeLast])
|
||||
fmt.Println("First and last removed:", u[sizeFirst:len(u)-sizeLast])
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
"Golfscript"(;n
|
||||
"Golfscript");n
|
||||
"Golfscript"(;);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def top = { it.size() > 1 ? it[0..-2] : '' }
|
||||
def tail = { it.size() > 1 ? it[1..-1] : '' }
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def testVal = 'upraisers'
|
||||
println """
|
||||
original: ${testVal}
|
||||
top: ${top(testVal)}
|
||||
tail: ${tail(testVal)}
|
||||
top&tail: ${tail(top(testVal))}
|
||||
"""
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
-- We define the functions to return an empty string if the argument is too
|
||||
-- short for the particular operation.
|
||||
|
||||
remFirst, remLast, remBoth :: String -> String
|
||||
|
||||
remFirst "" = ""
|
||||
remFirst cs = tail cs
|
||||
|
||||
remLast "" = ""
|
||||
remLast cs = init cs
|
||||
|
||||
remBoth (c:cs) = remLast cs
|
||||
remBoth _ = ""
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let s = "Some string."
|
||||
mapM_ (\f -> putStrLn . f $ s) [remFirst, remLast, remBoth]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
word = "knights"
|
||||
|
||||
main = do
|
||||
-- You can drop the first item
|
||||
-- using `tail`
|
||||
putStrLn (tail word)
|
||||
|
||||
-- The `init` function will drop
|
||||
-- the last item
|
||||
putStrLn (init word)
|
||||
|
||||
-- We can combine these two to drop
|
||||
-- the last and the first characters
|
||||
putStrLn (middle word)
|
||||
|
||||
-- You can combine functions using `.`,
|
||||
-- which is pronounced "compose" or "of"
|
||||
middle = init . tail
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
main :: IO ()
|
||||
main = mapM_ print $ [tail, init, init . tail] <*> ["knights"]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
100 LET S$="Knights"
|
||||
110 PRINT S$(2:)
|
||||
120 PRINT S$(:LEN(S$)-1)
|
||||
130 PRINT S$(2:LEN(S$)-1)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
procedure main()
|
||||
write(s := "knight"," --> ", s[2:0]) # drop 1st char
|
||||
write(s := "sock"," --> ", s[1:-1]) # drop last
|
||||
write(s := "brooms"," --> ", s[2:-1]) # drop both
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
write(s := "knight"," --> ", s[1] := "", s) # drop 1st char
|
||||
6
Task/Substring-Top-and-tail/J/substring-top-and-tail.j
Normal file
6
Task/Substring-Top-and-tail/J/substring-top-and-tail.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
}. 'knight' NB. drop first item
|
||||
night
|
||||
}: 'socks' NB. drop last item
|
||||
sock
|
||||
}: }. 'brooms' NB. drop first and last items
|
||||
room
|
||||
14
Task/Substring-Top-and-tail/Java/substring-top-and-tail.java
Normal file
14
Task/Substring-Top-and-tail/Java/substring-top-and-tail.java
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
public class RM_chars {
|
||||
public static void main( String[] args ){
|
||||
System.out.println( "knight".substring( 1 ) );
|
||||
System.out.println( "socks".substring( 0, 4 ) );
|
||||
System.out.println( "brooms".substring( 1, 5 ) );
|
||||
// first, do this by selecting a specific substring
|
||||
// to exclude the first and last characters
|
||||
|
||||
System.out.println( "knight".replaceAll( "^.", "" ) );
|
||||
System.out.println( "socks".replaceAll( ".$", "" ) );
|
||||
System.out.println( "brooms".replaceAll( "^.|.$", "" ) );
|
||||
// then do this using a regular expressions
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
DEFINE
|
||||
dropfirst == 1 drop;
|
||||
droplast == dup size pred take.
|
||||
|
||||
"abcd" dropfirst.
|
||||
"abcd" droplast.
|
||||
"abcd" dropfirst droplast.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
"一二三四五六七八九十"[1:]' => "二三四五六七八九十"
|
||||
|
||||
"一二三四五六七八九十"[:-1]' => "一二三四五六七八九"
|
||||
|
||||
"一二三四五六七八九十"[1:-1]' => "二三四五六七八九"
|
||||
|
||||
"a"[1:-1] # => ""
|
||||
|
|
@ -0,0 +1 @@
|
|||
"abc" | capture( ".(?<monkey>.*)." ).monkey => "b"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
julia> "My String"[2:end] # without first character
|
||||
"y String"
|
||||
|
||||
julia> "My String"[1:end-1] # without last character
|
||||
"My Strin"
|
||||
|
||||
julia> "My String"[2:end-1] # without first and last characters
|
||||
"y Strin"
|
||||
8
Task/Substring-Top-and-tail/K/substring-top-and-tail-1.k
Normal file
8
Task/Substring-Top-and-tail/K/substring-top-and-tail-1.k
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
s: "1234567890"
|
||||
"1234567890"
|
||||
s _di 0 /Delete 1st character
|
||||
"234567890"
|
||||
s _di -1+#s /Delete last character
|
||||
"123456789"
|
||||
(s _di -1+#s) _di 0 /String with both 1st and last character removed
|
||||
"23456789"
|
||||
8
Task/Substring-Top-and-tail/K/substring-top-and-tail-2.k
Normal file
8
Task/Substring-Top-and-tail/K/substring-top-and-tail-2.k
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
s: "1234567890"
|
||||
"1234567890"
|
||||
1 _ s /Delete 1st character
|
||||
"234567890"
|
||||
-1 _ s /Delete last character
|
||||
"123456789"
|
||||
1 - -1 _ s /Delete 1st and last character
|
||||
"23456789"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// version 1.0.6
|
||||
fun main(args: Array<String>) {
|
||||
val s = "Rosetta"
|
||||
println(s.drop(1))
|
||||
println(s.dropLast(1))
|
||||
println(s.drop(1).dropLast(1))
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
{def R rosetta} -> rosetta
|
||||
|
||||
{W.rest {R}} -> osetta
|
||||
{W.reverse {W.rest {W.reverse {R}}}} -> rosett
|
||||
{W.rest {W.reverse {W.rest {W.reverse {R}}}}} -> osett
|
||||
|
||||
or
|
||||
|
||||
{W.slice 1 {W.length {R}} {R}} -> osetta
|
||||
{W.slice 0 {- {W.length {R}} 1} {R}} -> rosett
|
||||
{W.slice 1 {- {W.length {R}} 1} {R}} -> osett
|
||||
|
||||
{def J ストリング} -> ストリング
|
||||
|
||||
{W.rest {J}} -> トリング
|
||||
{W.reverse {W.rest {W.reverse {J}}}} -> ストリン
|
||||
{W.rest {W.reverse {W.rest {W.reverse {J}}}}} -> トリン
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
local(str = 'The quick grey rhino jumped over the lazy green fox.')
|
||||
|
||||
// String with first character removed
|
||||
string_remove(#str,-startposition=1,-endposition=1)
|
||||
|
||||
// String with last character removed
|
||||
string_remove(#str,-startposition=#str->size,-endposition=#str->size)
|
||||
|
||||
// String with both the first and last characters removed
|
||||
string_remove(string_remove(#str,-startposition=#str->size,-endposition=#str->size),-startposition=1,-endposition=1)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
local(mystring = 'ÅÜÄÖカ')
|
||||
|
||||
#mystring -> remove(1,1)
|
||||
#mystring
|
||||
'<br />'
|
||||
#mystring -> remove(#mystring -> size,1)
|
||||
#mystring
|
||||
'<br />'
|
||||
#mystring -> remove(1,1)& -> remove(#mystring -> size,1)
|
||||
#mystring
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
string$ = "Rosetta Code"
|
||||
Print Mid$(string$, 2)
|
||||
Print Left$(string$, (Len(string$) - 1))
|
||||
Print Mid$(string$, 2, (Len(string$) - 2))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
put "pple" into x
|
||||
answer char 2 to len(x) of x // pple
|
||||
answer char 1 to -2 of x // ppl
|
||||
answer char 2 to -2 of x // ppl
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
make "s "|My string|
|
||||
print butfirst :s
|
||||
print butlast :s
|
||||
print butfirst butlast :s
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
:- object(top_and_tail).
|
||||
|
||||
:- public(test/1).
|
||||
test(String) :-
|
||||
sub_atom(String, 1, _, 0, MinusTop),
|
||||
write('String with first character cut: '), write(MinusTop), nl,
|
||||
sub_atom(String, 0, _, 1, MinusTail),
|
||||
write('String with last character cut: '), write(MinusTail), nl,
|
||||
sub_atom(String, 1, _, 1, MinusTopAndTail),
|
||||
write('String with first and last characters cut: '), write(MinusTopAndTail), nl.
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
| ?- top_and_tail::test('Rosetta').
|
||||
String with first character cut: osetta
|
||||
String with last character cut: Rosett
|
||||
String with first and last characters cut: osett
|
||||
yes
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
print (string.sub("knights",2)) -- remove the first character
|
||||
print (string.sub("knights",1,-2)) -- remove the last character
|
||||
print (string.sub("knights",2,-2)) -- remove the first and last characters
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
% String with first character removed
|
||||
str(2:end)
|
||||
% String with last character removed
|
||||
str(1:end-1)
|
||||
% String with both the first and last characters removed
|
||||
str(2:end-1)
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
.include "\SrcAll\Header.asm"
|
||||
.include "\SrcAll\BasicMacros.asm"
|
||||
.include "\SrcALL\AdvancedMacros.asm"
|
||||
.include "\SrcALL\MemoryMap.asm"
|
||||
|
||||
; .definelabel UserRam,0xA0010000 (this is defined in the header)
|
||||
|
||||
CursorX equ 0x100 ;offset from label UserRam
|
||||
CursorY equ 0x101 ;offset from label UserRam
|
||||
|
||||
main:
|
||||
jal Cls
|
||||
nop
|
||||
|
||||
la a0,MyString
|
||||
la a1,UserRam+0x1000
|
||||
push a0
|
||||
push a1
|
||||
jal strcpy
|
||||
addiu a0,1 ;branch delay slot - increment base address prior to branching
|
||||
pop a0 ;deliberately pop in the "wrong order"
|
||||
pop a1 ;because printString uses $a0
|
||||
|
||||
jal PrintString
|
||||
nop
|
||||
jal NewLine
|
||||
nop
|
||||
|
||||
|
||||
|
||||
la a0,MyString
|
||||
la a1,UserRam+0x1000
|
||||
|
||||
push a0
|
||||
push a1
|
||||
jal strcpy
|
||||
nop ;branch delay slot
|
||||
;after a strcpy, a0/a1 both point to the null terminator
|
||||
subiu a1,1
|
||||
move t0,zero
|
||||
sb t0,(a1)
|
||||
.ifdef buildPSX
|
||||
nop ;load delay slot
|
||||
.endif
|
||||
pop a0
|
||||
pop a1
|
||||
|
||||
jal PrintString
|
||||
nop
|
||||
jal NewLine
|
||||
nop
|
||||
|
||||
la a0,MyString
|
||||
la a1,UserRam+0x1000
|
||||
|
||||
push a0
|
||||
push a1
|
||||
jal strcpy
|
||||
addiu a0,1 ;branch delay slot
|
||||
;after a strcpy, a0/a1 both point to the null terminator
|
||||
subiu a1,1
|
||||
move t0,zero
|
||||
sb t0,(a1)
|
||||
.ifdef buildPSX
|
||||
nop ;load delay slot
|
||||
.endif
|
||||
pop a0
|
||||
pop a1
|
||||
|
||||
jal PrintString
|
||||
nop
|
||||
jal NewLine
|
||||
nop
|
||||
|
||||
|
||||
halt:
|
||||
nop
|
||||
j halt
|
||||
nop
|
||||
|
||||
|
||||
MyString:
|
||||
.ascii "Hello World"
|
||||
.byte 0
|
||||
.align 4
|
||||
|
||||
MyFont:
|
||||
|
||||
.ifdef buildn64
|
||||
.incbin "\ResN64\ChibiAkumas.fnt"
|
||||
.endif
|
||||
.ifdef buildPSX
|
||||
.incbin "\ResPSX\ChibiAkumas.fnt"
|
||||
.endif
|
||||
|
||||
.include "\SrcALL\graphics.asm"
|
||||
.include "\SrcAll\monitor.asm"
|
||||
.include "\SrcALL\Multiplatform_Math_Integer.asm"
|
||||
.include "\SrcALL\BasicFunctions_v2.asm"
|
||||
.include "\SrcN64\Footer.asm"
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
> s := "some string":
|
||||
> s[2..-1];
|
||||
"ome string"
|
||||
|
||||
> s[1..-2];
|
||||
"some strin"
|
||||
|
||||
> s[2..-2];
|
||||
"ome strin"
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> substring( s, 2 .. -1 );
|
||||
"ome string"
|
||||
|
||||
> substring( s, 1 .. -2 );
|
||||
"some strin"
|
||||
|
||||
> substring( s, 2 .. -2 );
|
||||
"ome strin"
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
> use StringTools in
|
||||
> SubString( s, 2 .. -1 );
|
||||
> SubString( s, 1 .. -1 );
|
||||
> SubString( s, 2 .. -2 )
|
||||
> end use;
|
||||
"ome string"
|
||||
|
||||
"some string"
|
||||
|
||||
"ome strin"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
StringDrop["input string",1]
|
||||
StringDrop["input string",-1]
|
||||
StringTake["input string",{2,-2}]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
test = "This thing"
|
||||
print test[1:]
|
||||
print test[:-1]
|
||||
print test[1:-1]
|
||||
11
Task/Substring-Top-and-tail/Neko/substring-top-and-tail.neko
Normal file
11
Task/Substring-Top-and-tail/Neko/substring-top-and-tail.neko
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/**
|
||||
Subtring/Top-Tail in Neko
|
||||
*/
|
||||
|
||||
var data = "[this is a test]"
|
||||
var len = $ssize(data)
|
||||
|
||||
$print(data, "\n")
|
||||
$print($ssub(data, 1, len - 1), "\n")
|
||||
$print($ssub(data, 0, len - 1), "\n")
|
||||
$print($ssub(data, 1, len - 2), "\n")
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module RemoveChars
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def str = "*A string*";
|
||||
def end = str.Remove(str.Length - 1); // from pos to end
|
||||
def beg = str.Remove(0, 1); // start pos, # of chars to remove
|
||||
def both = str.Trim(array['*']); // with Trim() you need to know what char's you're removing
|
||||
|
||||
WriteLine($"$str -> $beg -> $end -> $both");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
/**********************************************************************
|
||||
* 02.08.2013 Walter Pachl translated from REXX
|
||||
**********************************************************************/
|
||||
z = 'abcdefghijk'
|
||||
l=z.length()
|
||||
say ' the original string =' z
|
||||
If l>=1 Then Do
|
||||
Say 'string first character removed =' z.substr(2)
|
||||
say 'string last character removed =' z.left(l-1)
|
||||
End
|
||||
If l>=2 Then
|
||||
Say 'string first & last character removed =' z.substr(2,l-2)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(let (str "rosetta")
|
||||
;; strip first char
|
||||
(println (1 str))
|
||||
;; strip last char
|
||||
(println (0 -1 str))
|
||||
;; strip both first and last characters
|
||||
(println (1 -1 str)))
|
||||
10
Task/Substring-Top-and-tail/Nim/substring-top-and-tail.nim
Normal file
10
Task/Substring-Top-and-tail/Nim/substring-top-and-tail.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import unicode
|
||||
|
||||
let s = "Hänsel ««: 10,00€"
|
||||
echo "Original: ", s
|
||||
echo "With first character removed: ", s.runeSubStr(1)
|
||||
echo "With last character removed: ", s.runeSubStr(0, s.runeLen - 1)
|
||||
echo "With first and last characters removed: ", s.runeSubStr(1, s.runeLen - 2)
|
||||
# Using the runes type and slices
|
||||
let r = s.toRunes
|
||||
echo "With first and last characters removed (other way): ", r[1 .. ^2]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
let strip_first_char str =
|
||||
if str = "" then "" else
|
||||
String.sub str 1 ((String.length str) - 1)
|
||||
|
||||
let strip_last_char str =
|
||||
if str = "" then "" else
|
||||
String.sub str 0 ((String.length str) - 1)
|
||||
|
||||
let strip_both_chars str =
|
||||
match String.length str with
|
||||
| 0 | 1 | 2 -> ""
|
||||
| len -> String.sub str 1 (len - 2)
|
||||
|
||||
let () =
|
||||
print_endline (strip_first_char "knight");
|
||||
print_endline (strip_last_char "socks");
|
||||
print_endline (strip_both_chars "brooms");
|
||||
;;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
bundle Default {
|
||||
class TopTail {
|
||||
function : Main(args : System.String[]) ~ Nil {
|
||||
string := "test";
|
||||
string->SubString(1, string->Size() - 1)->PrintLine();
|
||||
string->SubString(string->Size() - 1)->PrintLine();
|
||||
string->SubString(1, string->Size() - 2)->PrintLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
: topAndTail(s)
|
||||
s right(s size 1-) println
|
||||
s left(s size 1-) println
|
||||
s extract(2, s size 1- ) println ;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
df(s)=concat(vecextract(Vec(s),1<<#s-2));
|
||||
dl(s)=concat(vecextract(Vec(s),1<<(#s-1)-1));
|
||||
db(s)=concat(vecextract(Vec(s),1<<(#s-1)-2));
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
echo substr("knight", 1), "\n"; // strip first character
|
||||
echo substr("socks", 0, -1), "\n"; // strip last character
|
||||
echo substr("brooms", 1, -1), "\n"; // strip both first and last characters
|
||||
?>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
declare s character (100) varying;
|
||||
s = 'now is the time to come to the aid of the party';
|
||||
if length(s) <= 2 then stop;
|
||||
put skip list ('First character removed=' || substr(s,2) );
|
||||
put skip list ('Last character removed=' || substr(s, 1, length(s)-1) );
|
||||
put skip list ('One character from each end removed=' ||
|
||||
substr(s, 2, length(s)-2) );
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
program topAndTail(output);
|
||||
var
|
||||
line: string(20);
|
||||
begin
|
||||
line := 'ABCDEF';
|
||||
|
||||
if length(line) > 1 then
|
||||
begin
|
||||
{ string with first character removed }
|
||||
writeLn(subStr(line, 2));
|
||||
{ index range expression: only possible for strings }
|
||||
{ _not_ designated `bindable` [e.g. `bindable string(20)`] }
|
||||
writeLn(line[2..length(line)]);
|
||||
|
||||
{ string with last character removed }
|
||||
|
||||
writeLn(subStr(line, 1, length(line) - 1));
|
||||
{ only legal with non-bindable strings: }
|
||||
writeLn(line[1..length(line)-1])
|
||||
end;
|
||||
|
||||
{ string with both the first and last characters removed }
|
||||
if length(line) > 2 then
|
||||
begin
|
||||
writeLn(subStr(line, 2, length(line) - 2));
|
||||
{ only for non-bindable strings: }
|
||||
writeLn(line[2..length(line)-1])
|
||||
end
|
||||
end.
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue