Substring: Difference between revisions

1,742 bytes removed ,  16 days ago
Add Ecstasy example
(Add Ecstasy example)
 
(13 intermediate revisions by 7 users not shown)
Line 1,133:
 
=={{header|BASIC}}==
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DIM baseString AS STRING, subString AS STRING, findString AS STRING
DIM m AS INTEGER, n AS INTEGER
 
baseString = "abcdefghijklmnopqrstuvwxyz"
n = 12
m = 5
 
' starting from n characters in and of m length;
subString = MID$(baseString, n, m)
PRINT subString
 
' starting from n characters in, up to the end of the string;
subString = MID$(baseString, n)
PRINT subString
 
' whole string minus last character;
subString = LEFT$(baseString, LEN(baseString) - 1)
PRINT subString
 
' starting from a known character within the string and of m length;
subString = MID$(baseString, INSTR(baseString, "b"), m)
PRINT subString
 
' starting from a known substring within the string and of m length.
findString = "pq"
subString = MID$(baseString, INSTR(baseString, findString), m)
PRINT subString
</syntaxhighlight>
 
{{out}}
<pre>
lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst
</pre>
 
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">0 READ N, M, S$ : L = LEN(S$) : GOSUB 1 : END : DATA 5,11,THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG,J,FOX
Line 1,192 ⟶ 1,153:
6 FOR I = 1 TO L : IF MID$(S$,I,LEN(F$)) = F$ THEN PRINT MID$(S$,I,M) : RETURN
7 NEXT : RETURN</syntaxhighlight>
 
==={{header|ASIC}}===
<syntaxhighlight lang="basic">REM Substring
Base$ = "abcdefghijklmnopqrstuvwxyz"
N = 12
M = 5
 
REM Starting from N characters in and of M length.
Sub$ = MID$(Base$, N, M)
PRINT Sub$
 
REM Starting from N characters in, up to the end of the string.
L = LEN(Base$)
L = L - N
L = L + 1
Sub$ = MID$(Base$, N, L)
PRINT Sub$
 
REM Whole string minus last character.
L = LEN(Base$)
L = L - 1
Sub$ = LEFT$(Base$, L)
PRINT Sub$
 
REM Starting from a known character within the string and of M length.
B = INSTR(Base$, "b")
Sub$ = MID$(Base$, B, M)
PRINT Sub$
 
REM Starting from a known substring within the string and of M length.
Find$ = "pq"
B = INSTR(Base$, Find$)
Sub$ = MID$(Base$, B, M)
PRINT Sub$
END
</syntaxhighlight>
{{out}}
<pre>
lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst
</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">c$ = "abcdefghijklmnopqrstuvwxyz"
Line 1,215 ⟶ 1,221:
pqrst</pre>
 
==={{header|TrueBBC BASIC}}===
<syntaxhighlight lang="qbasicbbcbasic">LET basestring$ = "abcdefghijklmnopqrstuvwxyzThe five boxing wizards jump quickly"
LET n% = 1210
LET m% = 5
! starting from n characters in and of m length;
REM starting from n characters in and of m length:
PRINT (basestring$)[n:n + m - 1]
substring$ = MID$(basestring$, n%, m%)
! starting from n characters in, up to the end of the string;
PRINT substring$
PRINT (basestring$)[n:MAXNUM]
! whole string minus last character;
REM starting from n characters in, up to the end of the string:
PRINT (basestring$)[1:LEN(basestring$) - 1]
substring$ = MID$(basestring$, n%)
! starting from a known character within the string and of m length;
PRINT substring$
PRINT (basestring$)[POS(basestring$,"b"):POS(basestring$,"b") + m - 1]
! starting from a known subString$ within the string and of m length.
REM whole string minus last character:
LET findstring$ = "pq"
substring$ = LEFT$(basestring$)
PRINT (basestring$)[POS(basestring$,findstring$):POS(basestring$,findstring$) + m - 1]
PRINT substring$
END</syntaxhighlight>
REM starting from a known character within the string and of m length:
char$ = "w"
substring$ = MID$(basestring$, INSTR(basestring$, char$), m%)
PRINT substring$
REM starting from a known substring within the string and of m length:
find$ = "iz"
substring$ = MID$(basestring$, INSTR(basestring$, find$), m%)
PRINT substring$</syntaxhighlight>
{{out}}
<pre>boxin
boxing wizards jump quickly
The five boxing wizards jump quickl
wizar
izard</pre>
 
==={{header|Commodore BASIC}}===
Line 1,283 ⟶ 1,305:
STARTING FROM 'FOX' AND OF 11 LENGTH:
FOX JUMPS OVER THE LAZY DOG
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim s As String = "123456789"
Dim As Integer n = 3, m = 4
Print Mid(s, n, m)
Print Mid(s, n)
Print Left(s, Len(s) - 1)
'start from "5" say
Print Mid(s, Instr(s, "5"), m)
' start from "12" say
Print Mid(s, Instr(s, "12"), m)
Sleep</syntaxhighlight>
 
{{out}}
<pre>
3456
3456789
12345678
5678
1234
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
void local fn DoIt
CFStringRef string = @"abcdefghijklmnopqrstuvwxyz"
NSLog(@"%@",mid(string,3,6))
NSLog(@"%@",fn StringSubstringFromIndex( string, 10 ))
NSLog(@"%@",left(string,len(string)-1))
CFRange range = fn StringRangeOfString( string, @"r" )
NSLog(@"%@",mid(string,range.location,6))
range = fn StringRangeOfString( string, @"pqr" )
NSLog(@"%@",mid(string,range.location,7))
end fn
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
{{out}}
<pre>
defghi
klmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
rstuvw
pqrstuv
</pre>
 
==={{header|Gambas}}===
'''[https://gambas-playground.proko.eu/?gist=d4baf4adccd2220f63a1019695e072b0 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
 
Print Mid(sString, 11, 5) 'Starting from n characters in and of m length
Print Mid(sString, 17) 'Starting from n characters in, up to the end of the string
Print Left(sString, -1) 'Whole string minus last character
Print Mid(sString, InStr(sString, "B"), 9) 'Starting from a known character within the string and of m length
Print Mid(sString, InStr(sString, "OVER"), 8) 'Starting from a known substring within the string and of m length
 
End</syntaxhighlight>
Output:
<pre>
BROWN
FOX JUMPS OVER THE LAZY DOG
THE QUICK BROWN FOX JUMPS OVER THE LAZY DO
BROWN FOX
OVER THE
</pre>
 
Line 1,295 ⟶ 1,393:
170 LET I=POS(A$,"ijk")
180 PRINT A$(I:I+M-1)</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
<syntaxhighlight lang="lb">'These tasks can be completed with various combinations of Liberty Basic's
'built in Mid$()/ Instr()/ Left$()/ Right$()/ and Len() functions, but these
'examples only use the Mid$()/ Instr()/ and Len() functions.
 
baseString$ = "Thequickbrownfoxjumpsoverthelazydog."
n = 12
m = 5
 
'starting from n characters in and of m length
Print Mid$(baseString$, n, m)
 
'starting from n characters in, up to the end of the string
Print Mid$(baseString$, n)
 
'whole string minus last character
Print Mid$(baseString$, 1, (Len(baseString$) - 1))
 
'starting from a known character within the string and of m length
Print Mid$(baseString$, Instr(baseString$, "f", 1), m)
 
'starting from a known substring within the string and of m length
Print Mid$(baseString$, Instr(baseString$, "jump", 1), m)</syntaxhighlight>
 
==={{header|Nascom BASIC}}===
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Substring
20 BAS$="abcdefghijklmnopqrstuvwxyz"
30 N=12:M=5
40 REM Starting from N characters in
50 REM and of M length
60 SB$=MID$(BAS$,N,M)
70 PRINT SB$
80 REM Starting from N characters in,
90 REM up to the end of the string
100 SB$=MID$(BAS$,N,LEN(BAS$)-N+1)
110 PRINT SB$
120 REM Whole string minus last character
130 SB$=LEFT$(BAS$,LEN(BAS$)-1)
140 PRINT SB$
150 REM Starting from a known character
160 REM within the string and of M length
170 A$=BAS$:B$="b":GOSUB 270
180 SB$=MID$(BAS$,C,M)
190 PRINT SB$
200 REM Starting from a known substring
210 REM within the string and of M length
220 A$=BAS$:B$="pq":GOSUB 270
230 SB$=MID$(BAS$,C,M)
240 PRINT SB$
250 END
260 REM ** INSTR subroutine
270 LB=LEN(B$):C=0
280 FOR I=1 TO LEN(A$)-LB+1
290 IF MID$(A$,I,LB)=B$ THEN C=I:RETURN
300 NEXT I
310 RETURN
</syntaxhighlight>
{{out}}
<pre>
lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst
</pre>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">If OpenConsole()
 
Define baseString.s, m, n
baseString = "Thequickbrownfoxjumpsoverthelazydog."
n = 12
m = 5
;Display the substring starting from n characters in and of m length.
PrintN(Mid(baseString, n, m))
;Display the substring starting from n characters in, up to the end of the string.
PrintN(Mid(baseString, n)) ;or PrintN(Right(baseString, Len(baseString) - n))
;Display the substring whole string minus last character
PrintN(Left(baseString, Len(baseString) - 1))
;Display the substring starting from a known character within the string and of m length.
PrintN(Mid(baseString, FindString(baseString, "b", 1), m))
 
;Display the substring starting from a known substring within the string and of m length.
PrintN(Mid(baseString, FindString(baseString, "ju", 1), m))
 
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>wnfox
wnfoxjumpsoverthelazydog.
Thequickbrownfoxjumpsoverthelazydog
brown
jumps</pre>
 
==={{header|QB64}}===
<syntaxhighlight lang="qb64">
DefStr S
DefInt I
string1 = "abcdefghijklmnopqrstuvwxyz"
substring = "klm"
Dim Achar As String * 1
Istart = 6
Ilength = 10
Achar = "c"
 
' starting from n characters in and of m length;
Print Mid$(string1, Istart, Ilength)
' starting from n characters in, up to the end of the string;
Print Mid$(string1, Istart)
Print Right$(string1, Len(string1) - Istart + 1)
' whole string minus the last character;
Print Left$(string1, Len(string1) - 1)
Print Mid$(string1, 1, Len(string1) - 1)
' starting from a known character within the string and of m length;
Print Mid$(string1, InStr(string1, Achar), Ilength)
' starting from a known substring within the string and of m length.
Print Mid$(string1, InStr(string1, substring), Ilength)
End
</syntaxhighlight>
 
==={{header|QuickBASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">DIM baseString AS STRING, subString AS STRING, findString AS STRING
DIM m AS INTEGER, n AS INTEGER
 
baseString = "abcdefghijklmnopqrstuvwxyz"
n = 12
m = 5
 
' starting from n characters in and of m length;
subString = MID$(baseString, n, m)
PRINT subString
 
' starting from n characters in, up to the end of the string;
subString = MID$(baseString, n)
PRINT subString
 
' whole string minus last character;
subString = LEFT$(baseString, LEN(baseString) - 1)
PRINT subString
 
' starting from a known character within the string and of m length;
subString = MID$(baseString, INSTR(baseString, "b"), m)
PRINT subString
 
' starting from a known substring within the string and of m length.
findString = "pq"
subString = MID$(baseString, INSTR(baseString, findString), m)
PRINT subString
</syntaxhighlight>
 
{{out}}
<pre>
lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst
</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">n = 2
m = 3
s$ = "abcd"
a$ = mid$(a$,n,m) ' starting from n characters in and of m length
a$ = mid$(a$,n) ' starting from n characters in, up to the end of the string
a$ = Print mid$(a$,1,(len(a$)-1)) ' whole string minus last character
a$ = mid$(a$,instr(a$,s$,1),m) ' starting from a known character within the string and of m length
a$ = mid$(a$,instr(a$,s$,1), m) ' starting from a known substring within the string and of m length.</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET basestring$ = "abcdefghijklmnopqrstuvwxyz"
LET n = 12
LET m = 5
! starting from n characters in and of m length;
PRINT (basestring$)[n:n + m - 1]
! starting from n characters in, up to the end of the string;
PRINT (basestring$)[n:MAXNUM]
! whole string minus last character;
PRINT (basestring$)[1:LEN(basestring$) - 1]
! starting from a known character within the string and of m length;
PRINT (basestring$)[POS(basestring$,"b"):POS(basestring$,"b") + m - 1]
! starting from a known subString$ within the string and of m length.
LET findstring$ = "pq"
PRINT (basestring$)[POS(basestring$,findstring$):POS(basestring$,findstring$) + m - 1]
END</syntaxhighlight>
 
==={{header|VBA}}===
{{trans|Phix}}<syntaxhighlight lang="vb">Public Sub substring()
'(1) starting from n characters in and of m length;
'(2) starting from n characters in, up to the end of the string;
'(3) whole string minus last character;
'(4) starting from a known character within the string and of m length;
'(5) starting from a known substring within the string and of m length.
sentence = "the last thing the man said was the"
n = 10: m = 5
'(1)
Debug.Print Mid(sentence, n, 5)
'(2)
Debug.Print Right(sentence, Len(sentence) - n + 1)
'(3)
Debug.Print Left(sentence, Len(sentence) - 1)
'(4)
k = InStr(1, sentence, "m")
Debug.Print Mid(sentence, k, 5)
'(5)
k = InStr(1, sentence, "aid")
Debug.Print Mid(sentence, k, 5)
End Sub</syntaxhighlight>{{out}}
<pre>thing
thing the man said was the
the last thing the man said was th
man s
aid w</pre>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">
s = "rosettacode.org"
 
'starting from n characters in and of m length
WScript.StdOut.WriteLine Mid(s,8,4)
 
'starting from n characters in, up to the end of the string
WScript.StdOut.WriteLine Mid(s,8,Len(s)-7)
 
'whole string minus last character
WScript.StdOut.WriteLine Mid(s,1,Len(s)-1)
 
'starting from a known character within the string and of m length
WScript.StdOut.WriteLine Mid(s,InStr(1,s,"c"),4)
 
'starting from a known substring within the string and of m length
WScript.StdOut.WriteLine Mid(s,InStr(1,s,"ose"),6)
</syntaxhighlight>
 
{{Out}}
<pre>
code
code.org
rosettacode.or
code
osetta
</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="yabasic">c$ = "abcdefghijklmnopqrstuvwxyz"
n = 12
m = 5
// starting from n characters in and of m length;
print mid$(c$, n, m)
// starting from n characters in, up to the end of the string;
print mid$(c$, n)
// whole string minus last character;
print left$(c$, len(c$) - 1)
// starting from a known character within the string and of m length;
print mid$(c$, instr(c$, "b"), m)
// starting from a known substring within the string and of m length.
f$ = "pq"
print mid$(c$, instr(c$, f$), m)
end</syntaxhighlight>
{{out}}
<pre>lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst</pre>
 
==={{header|ZX Spectrum Basic}}===
Line 1,331 ⟶ 1,707:
ghijklm
ijklmno</pre>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> basestring$ = "The five boxing wizards jump quickly"
n% = 10
m% = 5
REM starting from n characters in and of m length:
substring$ = MID$(basestring$, n%, m%)
PRINT substring$
REM starting from n characters in, up to the end of the string:
substring$ = MID$(basestring$, n%)
PRINT substring$
REM whole string minus last character:
substring$ = LEFT$(basestring$)
PRINT substring$
REM starting from a known character within the string and of m length:
char$ = "w"
substring$ = MID$(basestring$, INSTR(basestring$, char$), m%)
PRINT substring$
REM starting from a known substring within the string and of m length:
find$ = "iz"
substring$ = MID$(basestring$, INSTR(basestring$, find$), m%)
PRINT substring$</syntaxhighlight>
{{out}}
<pre>boxin
boxing wizards jump quickly
The five boxing wizards jump quickl
wizar
izard</pre>
 
=={{header|BQN}}==
Line 2,032 ⟶ 2,375:
=={{header|EasyLang}}==
 
<syntaxhighlight lang="text">a$ = "2019-05-22 22:54:22"
a$ = timestr systime
print substr a$ 12 5
print substr a$ 12 -199
#
a$ = "Hallo Österreich!"
print substr a$ 1 (len a$ - 1)
#
c$ = "Ö"
m = 2
i = 1
while substr a$ i 1 <> c$
i += 1
.
print substr a$ i m
#
c$ = "re"
m = 5
i = 1
while substr a$ i len c$ <> c$
i += 1
.
print substr a$ i m
</syntaxhighlight>
<pre>
22:54
22:54:22
</pre>
 
=={{header|ECL}}==
Line 2,076 ⟶ 2,435:
*/
</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="ecstasy">
module Substrings {
void run(String[] args = []) {
@Inject Console console;
if (args.size < 4) {
console.print(
$|
|Usage:
|
| xec Substrings <str> <offset> <count> <substr>
|
);
return;
}
 
String s = args[0];
Int n = new Int(args[1]);
Int m = new Int(args[2]);
String sub = args[3];
Char c = sub[0];
 
console.print($|
|{s .quoted()=}
|{substring(s, n, m ).quoted()=}
|{substring(s, n ).quoted()=}
|{substring(s ).quoted()=}
|{substring(s, c, m ).quoted()=}
|{substring(s, sub, m).quoted()=}
|
);
}
 
// starting from n characters in and of m length
static String substring(String s, Int n, Int m) {
assert 0 <= n <= n+m;
return n < s.size ? s[n..<(n+m).notGreaterThan(s.size)] : "";
}
 
// starting from n characters in, up to the end of the string
static String substring(String s, Int n) {
assert 0 <= n;
return s.substring(n);
}
 
// whole string minus the last character
static String substring(String s) {
return s.size > 1 ? s[0..<s.size-1] : "";
}
 
// starting from a known character within the string and of m length
static String substring(String s, Char c, Int m){
assert 0 <= m;
return substring(s, s.indexOf(c) ?: 0, m);
}
 
// starting from a known substring within the string and of m length
static String substring(String s, String sub, Int m){
assert 0 <= m;
return substring(s, s.indexOf(sub) ?: 0, m);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
x$ xec doc/examples/Substrings scaryaardvark 5 4 ard
 
s .quoted()="scaryaardvark"
substring(s, n, m ).quoted()="aard"
substring(s, n ).quoted()="aardvark"
substring(s ).quoted()="scaryaardvar"
substring(s, c, m ).quoted()="arya"
substring(s, sub, m).quoted()="ardv"
</pre>
 
=={{header|Eero}}==
Line 2,397 ⟶ 2,832:
s[pos(c, s)..pos(c, s)+m]
s[pos(p, s)..pos(p, s)+m]</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim s As String = "123456789"
Dim As Integer n = 3, m = 4
Print Mid(s, n, m)
Print Mid(s, n)
Print Left(s, Len(s) - 1)
'start from "5" say
Print Mid(s, Instr(s, "5"), m)
' start from "12" say
Print Mid(s, Instr(s, "12"), m)
Sleep</syntaxhighlight>
 
{{out}}
<pre>
3456
3456789
12345678
5678
1234
</pre>
 
=={{header|Frink}}==
Line 2,447 ⟶ 2,859:
cd
cd
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
void local fn DoIt
CFStringRef string = @"abcdefghijklmnopqrstuvwxyz"
NSLog(@"%@",mid(string,3,6))
NSLog(@"%@",fn StringSubstringFromIndex( string, 10 ))
NSLog(@"%@",left(string,len(string)-1))
CFRange range = fn StringRangeOfString( string, @"r" )
NSLog(@"%@",mid(string,range.location,6))
range = fn StringRangeOfString( string, @"pqr" )
NSLog(@"%@",mid(string,range.location,7))
end fn
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
{{out}}
<pre>
defghi
klmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
rstuvw
pqrstuv
</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=d4baf4adccd2220f63a1019695e072b0 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
 
Print Mid(sString, 11, 5) 'Starting from n characters in and of m length
Print Mid(sString, 17) 'Starting from n characters in, up to the end of the string
Print Left(sString, -1) 'Whole string minus last character
Print Mid(sString, InStr(sString, "B"), 9) 'Starting from a known character within the string and of m length
Print Mid(sString, InStr(sString, "OVER"), 8) 'Starting from a known substring within the string and of m length
 
End</syntaxhighlight>
Output:
<pre>
BROWN
FOX JUMPS OVER THE LAZY DOG
THE QUICK BROWN FOX JUMPS OVER THE LAZY DO
BROWN FOX
OVER THE
</pre>
 
Line 2,736 ⟶ 3,095:
shmal</syntaxhighlight>
 
Or, probably more efficient when the stringdesired substring is large:
 
<syntaxhighlight lang="j"> (,.3 5)];.0 'Marshmallow'
Line 3,283 ⟶ 3,642:
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$txt = The langLang programming language!
 
$n = 9
Line 3,299 ⟶ 3,658:
 
fn.println(fn.substring($txt, 0, parser.op(fn.len($txt) - 1)))
# Output: The langLang programming language
 
fn.println(fn.substring($txt, fn.indexOf($txt, $c), parser.op(fn.indexOf($txt, $c) + $m)))
Line 3,373 ⟶ 3,732:
"efghi"
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">'These tasks can be completed with various combinations of Liberty Basic's
'built in Mid$()/ Instr()/ Left$()/ Right$()/ and Len() functions, but these
'examples only use the Mid$()/ Instr()/ and Len() functions.
 
baseString$ = "Thequickbrownfoxjumpsoverthelazydog."
n = 12
m = 5
 
'starting from n characters in and of m length
Print Mid$(baseString$, n, m)
 
'starting from n characters in, up to the end of the string
Print Mid$(baseString$, n)
 
'whole string minus last character
Print Mid$(baseString$, 1, (Len(baseString$) - 1))
 
'starting from a known character within the string and of m length
Print Mid$(baseString$, Instr(baseString$, "f", 1), m)
 
'starting from a known substring within the string and of m length
Print Mid$(baseString$, Instr(baseString$, "jump", 1), m)</syntaxhighlight>
 
=={{header|Lingo}}==
Line 4,096 ⟶ 4,431:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--(phixonline)-->
<!--<syntaxhighlight lang="phix">-->
<syntaxhighlight lang="phix">
<span style="color: #000080;font-style:italic;">--(1) starting from n characters in and of m length;
--(21) starting from n characters in, up to the endand of them stringlength;
--(2) starting from n characters in, up to the end of the string;
--(3) whole string minus last character;
--(43) startingwhole fromstring aminus knownlast character within the string and of m length;
--(54) starting from a known substringcharacter within the string and of m length.; </span>
--(5) starting from a known substring within the string and of m length.
<span style="color: #008080;">constant</span> <span style="color: #000000;">sentence</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"the last thing the man said was the"</span><span style="color: #0000FF;">,</span>
constant sentence = "the last thing the man said was the",
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span>
n = 10, m = 5
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span>
integer k, l
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
l = n+m-1
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if l<=length(sentence) then
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (1)</span>
?sentence[n..l] -- (1)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if n<=length(sentence) then
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (2) or [n..$]</span>
?sentence[n..-1] -- (2) or [n..$]
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if length(sentence)>0 then
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (3) or [1..$-1]</span>
?sentence[1..-2] -- (3) or [1..$-1]
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'m'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
k = find('m',sentence)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
l = k+m-1
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if l<=length(sentence) then
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (4)</span>
?sentence[k..l] -- (4)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aid"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span>
k = match("aid",sentence)
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
l = k+m-1
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if l<=length(sentence) then
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">..</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span> <span style="color: #000080;font-style:italic;">-- (5)</span>
?sentence[k..l] -- (5)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
Line 4,136 ⟶ 4,472:
</pre>
Alternative version with no error handling, for those in a hurry (same ouput):
<!--<syntaxhighlight lang="phix">-->
?sentence[n..n+m-1]
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">..</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
?sentence[n..-1]
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
?sentence[1..-2]
<span style="color: #0000FF;">?</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
?(sentence[find('m',sentence)..$])[1..m]
<span style="color: #0000FF;">?(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'m'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)..$])[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span>
?(sentence[match("aid",sentence)..$])[1..m]
<span style="color: #0000FF;">?(</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"aid"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sentence</span><span style="color: #0000FF;">)..$])[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span>
<!--</syntaxhighlight>-->
If sentence is UTF-8 or UTF-16, you should explicitly use sequence utf32 = utf8_to_utf32(string utf8) or sequence utf32 = utf16_to_utf32(sequence utf16) before any slicing or find()/match(), and string utf8 = utf32_to_utf8(sequence utf32) or sequence utf16 = utf32_to_utf16(sequence utf32) before display. Note that unicode does not normally display correctly on a standard Windows console, but is fine in a GUI or Linux console or a web browser.
 
=={{header|Phixmonti}}==
Line 4,322 ⟶ 4,659:
true
</syntaxhighlight>
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasic">If OpenConsole()
 
Define baseString.s, m, n
baseString = "Thequickbrownfoxjumpsoverthelazydog."
n = 12
m = 5
;Display the substring starting from n characters in and of m length.
PrintN(Mid(baseString, n, m))
;Display the substring starting from n characters in, up to the end of the string.
PrintN(Mid(baseString, n)) ;or PrintN(Right(baseString, Len(baseString) - n))
;Display the substring whole string minus last character
PrintN(Left(baseString, Len(baseString) - 1))
;Display the substring starting from a known character within the string and of m length.
PrintN(Mid(baseString, FindString(baseString, "b", 1), m))
 
;Display the substring starting from a known substring within the string and of m length.
PrintN(Mid(baseString, FindString(baseString, "ju", 1), m))
 
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf</syntaxhighlight>
{{out}}
<pre>wnfox
wnfoxjumpsoverthelazydog.
Thequickbrownfoxjumpsoverthelazydog
brown
jumps</pre>
 
=={{header|Python}}==
Line 4,382 ⟶ 4,684:
>>></syntaxhighlight>
 
=={{header|QB64}}==
<syntaxhighlight lang="qb64">
DefStr S
DefInt I
string1 = "abcdefghijklmnopqrstuvwxyz"
substring = "klm"
Dim Achar As String * 1
Istart = 6
Ilength = 10
Achar = "c"
 
' starting from n characters in and of m length;
Print Mid$(string1, Istart, Ilength)
' starting from n characters in, up to the end of the string;
Print Mid$(string1, Istart)
Print Right$(string1, Len(string1) - Istart + 1)
' whole string minus the last character;
Print Left$(string1, Len(string1) - 1)
Print Mid$(string1, 1, Len(string1) - 1)
' starting from a known character within the string and of m length;
Print Mid$(string1, InStr(string1, Achar), Ilength)
' starting from a known substring within the string and of m length.
Print Mid$(string1, InStr(string1, substring), Ilength)
End
</syntaxhighlight>
=={{header|Quackery}}==
 
Line 4,706 ⟶ 4,983:
DSPLY Universum
DSPLY bewegt
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ → string n m char sub ≪
string n DUP m + 1 - SUB
string n OVER SIZE SUB
string 1 OVER SIZE 1 - SUB
string DUP char POS DUP m + 1 - SUB
string DUP sub POS DUP m + 1 - SUB
≫ ≫ '''SHOWC''' STO
|
''( string start length char sub -- sub1 .. sub5 )''
from n characters in and of m length
from n characters in, up to the end of the string
whole string minus the last character
from a character within the string and of m length
from a substring within the string and of m length
|}
The following piece of code will deliver what is required:
"abcdefgh" 2 3 "d" "cd" '''SHOWC'''
{{out}}
<pre>
5: bcd
4: bcdefgh
3: abcdefg
2: def
1: cde
</pre>
 
Line 4,719 ⟶ 5,030:
puts str[str.index('de'), m] #=> def
puts str[/a.*d/] #=> abcd</syntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">n = 2
m = 3
s$ = "abcd"
a$ = mid$(a$,n,m) ' starting from n characters in and of m length
a$ = mid$(a$,n) ' starting from n characters in, up to the end of the string
a$ = Print mid$(a$,1,(len(a$)-1)) ' whole string minus last character
a$ = mid$(a$,instr(a$,s$,1),m) ' starting from a known character within the string and of m length
a$ = mid$(a$,instr(a$,s$,1), m) ' starting from a known substring within the string and of m length.</syntaxhighlight>
 
=={{header|Rust}}==
Line 5,339 ⟶ 5,640:
string s_fromlo_for_m = s[index_of_lo:index_of_lo + m];
</syntaxhighlight>
 
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Public Sub substring()
'(1) starting from n characters in and of m length;
'(2) starting from n characters in, up to the end of the string;
'(3) whole string minus last character;
'(4) starting from a known character within the string and of m length;
'(5) starting from a known substring within the string and of m length.
sentence = "the last thing the man said was the"
n = 10: m = 5
'(1)
Debug.Print Mid(sentence, n, 5)
'(2)
Debug.Print Right(sentence, Len(sentence) - n + 1)
'(3)
Debug.Print Left(sentence, Len(sentence) - 1)
'(4)
k = InStr(1, sentence, "m")
Debug.Print Mid(sentence, k, 5)
'(5)
k = InStr(1, sentence, "aid")
Debug.Print Mid(sentence, k, 5)
End Sub</syntaxhighlight>{{out}}
<pre>thing
thing the man said was the
the last thing the man said was th
man s
aid w</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
s = "rosettacode.org"
 
'starting from n characters in and of m length
WScript.StdOut.WriteLine Mid(s,8,4)
 
'starting from n characters in, up to the end of the string
WScript.StdOut.WriteLine Mid(s,8,Len(s)-7)
 
'whole string minus last character
WScript.StdOut.WriteLine Mid(s,1,Len(s)-1)
 
'starting from a known character within the string and of m length
WScript.StdOut.WriteLine Mid(s,InStr(1,s,"c"),4)
 
'starting from a known substring within the string and of m length
WScript.StdOut.WriteLine Mid(s,InStr(1,s,"ose"),6)
</syntaxhighlight>
 
{{Out}}
<pre>
code
code.org
rosettacode.or
code
osetta
</pre>
 
=={{header|V (Vlang)}}==
Line 5,462 ⟶ 5,704:
{{trans|Go}}
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Str
var s = "αβγδεζηθ"
Line 5,527 ⟶ 5,769:
cde
</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">c$ = "abcdefghijklmnopqrstuvwxyz"
n = 12
m = 5
// starting from n characters in and of m length;
print mid$(c$, n, m)
// starting from n characters in, up to the end of the string;
print mid$(c$, n)
// whole string minus last character;
print left$(c$, len(c$) - 1)
// starting from a known character within the string and of m length;
print mid$(c$, instr(c$, "b"), m)
// starting from a known substring within the string and of m length.
f$ = "pq"
print mid$(c$, instr(c$, f$), m)
end</syntaxhighlight>
{{out}}
<pre>lmnop
lmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcdef
pqrst</pre>
 
=={{header|Yorick}}==
162

edits