Text between: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 4 users not shown)
Line 2,510:
print textBetween( thisText, startString, endString )
</syntaxhighlight>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ dup $ "start" = iff
drop done
tuck over findseq
tuck over found iff
[ unrot dip size +
split nip ]
done
2drop drop $ "" ] is from-start ( $ $ --> $ )
 
[ dup $ "end" = iff drop done
over findseq split drop ] is to-end ( $ $ --> $ )
 
[ dip from-start to-end ] is between ( $ $ $ --> $ )
 
[ char " tuck join join
echo$ cr ] is quote$ ( $ --> )
 
[ 3 pack dup
dip unpack unpack
rot say " Text: " quote$
swap say " Start: " quote$
say " End: " quote$
between
say "Result: " quote$
cr ] is task ( $ $ $ --> $ )
 
$ "Hello Rosetta Code world" $ "Hello " $ " world" task
$ "Hello Rosetta Code world" $ "start" $ " world" task
$ "Hello Rosetta Code world" $ "Hello " $ "end" task
$ "Hello Rosetta Code world" $ "Hello " $ "end" task
$ '</div><div style=\"chinese\">???</div>'
$ '<div style=\"chinese\">' $ "</div>" task
$ '<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">'
$ "<text>" $ "<table>" task
$ '<table style=\"myTable\"><tr><td>hello world</td></tr></table>'
$ "<table>" $ "</table>" task
$ "The quick brown fox jumps over the lazy other fox"
$ "quick " $ " fox" task
$ "One fish two fish red fish blue fish"
$ "fish " $ " red" task
$ "FooBarBazFooBuxQuux" $ "Foo" $ "Foo" task</syntaxhighlight>
 
{{out}}
 
<pre style="scroll: overflow; height: 20em"> Text: "Hello Rosetta Code world"
Start: "Hello "
End: " world"
Result: "Rosetta Code"
 
Text: "Hello Rosetta Code world"
Start: "start"
End: " world"
Result: "Hello Rosetta Code"
 
Text: "Hello Rosetta Code world"
Start: "Hello "
End: "end"
Result: "Rosetta Code world"
 
Text: "Hello Rosetta Code world"
Start: "Hello "
End: "end"
Result: "Rosetta Code world"
 
Text: "</div><div style=\"chinese\">???</div>"
Start: "<div style=\"chinese\">"
End: "</div>"
Result: "???"
 
Text: "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">"
Start: "<text>"
End: "<table>"
Result: "Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">"
 
Text: "<table style=\"myTable\"><tr><td>hello world</td></tr></table>"
Start: "<table>"
End: "</table>"
Result: ""
 
Text: "The quick brown fox jumps over the lazy other fox"
Start: "quick "
End: " fox"
Result: "brown"
 
Text: "One fish two fish red fish blue fish"
Start: "fish "
End: " red"
Result: "two fish"
 
Text: "FooBarBazFooBuxQuux"
Start: "Foo"
End: "Foo"
Result: "BarBaz"</pre>
 
=={{header|Racket}}==
Line 2,851 ⟶ 2,947:
Output = "Rosetta Code world"
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
« → start end
« '''CASE'''
start "start" == '''THEN''' 1 '''END'''
DUP start POS '''THEN''' LASTARG start SIZE + '''END'''
DUP SIZE 1 +
'''END'''
OVER SIZE SUB
1 OVER end POS 1 -
'''IF''' DUP 0 < end "end" == OR '''THEN''' DROP OVER SIZE '''END'''
SUB
» » '<span style="color:blue">BTWN</span>' STO <span style="color:grey">''@ ( "text" "start" "end" -- "text_between" )''</span>
 
=={{header|Ruby}}==
Line 3,698 ⟶ 3,808:
result: 'BarBaz'</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn text_between(str string, start string, end string) string {
if str == "" || start == "" || end == "" {
return str
Line 3,806 ⟶ 3,916:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var textBetween = Fn.new { |str, start, end|
Line 3,899 ⟶ 4,009:
End delimiter: "Foo"
Output: "BarBaz"
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib; \for StrFind, StrLen, Print
 
proc TextBetween(Str, StartStr, EndStr);
char Str, StartStr, EndStr;
int EndInx, I, Addr;
[if StrFind(StartStr, "start") = 0 then \another delimiter is given
[Addr:= StrFind(Str, StartStr);
if Addr # 0 then \if delimiter found then
Str:= Addr + StrLen(StartStr) \ start output string after it
else Str:= Str + StrLen(Str);
];
EndInx:= StrLen(Str) - 1;
if StrFind(EndStr, "end") = 0 then \another delimiter is given
[Addr:= StrFind(Str, EndStr);
if Addr # 0 then \if delimiter found then
EndInx:= Addr - Str - 1; \ end output string before it
];
ChOut(0, ^");
for I:= 0 to EndInx do
ChOut(0, Str(I));
ChOut(0, ^"); CrLf(0);
];
 
int Texts, Starts, Ends, I;
[Texts:= [
"Hello Rosetta Code world",
"Hello Rosetta Code world",
"Hello Rosetta Code world",
"</div><div style=^"chinese^">你好嗎</div>",
"<text>Hello <span>Rosetta Code</span> world</text><table style=^"myTable^">",
"<table style=^"myTable^"><tr><td>hello world</td></tr></table>",
"The quick brown fox jumps over the lazy other fox",
"One fish two fish red fish blue fish",
"FooBarBazFooBuxQuux"];
Starts:= [
"Hello ", "start", "Hello ", "<div style=^"chinese^">",
"<text>", "<table>", "quick ", "fish ", "Foo"];
Ends:= [
" world", " world", "end", "</div>",
"<table>", "</table>", " fox", " red", "Foo"];
for I:= 0 to 9-1 do
[Print("Example %d: ", I+1);
TextBetween(Texts(I), Starts(I), Ends(I));
];
]</syntaxhighlight>
{{out}}
<pre>
Example 1: "Rosetta Code"
Example 2: "Hello Rosetta Code"
Example 3: "Rosetta Code world"
Example 4: "你好嗎"
Example 5: "Hello <span>Rosetta Code</span> world</text><table style="myTable">"
Example 6: ""
Example 7: "brown"
Example 8: "two fish"
Example 9: "BarBaz"
</pre>
 
9,476

edits