Text between: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(11 intermediate revisions by 8 users not shown) | |||
Line 92:
{{trans|Python}}
<
X.try
Int startIndex
Line 127:
V startString = :argv[2]
V endString = :argv[3]
print(textBetween(thisText, startString, endString))</
=={{header|Action!}}==
<
CHAR ARRAY tmp(255)
Line 200:
Test(8,"One fish two fish red fish blue fish","fish "," red")
Test(9,"FooBarBazFooBuxQuux","Foo","Foo")
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Text_between.png Screenshot from Atari 8-bit computer]
Line 224:
=={{header|Ada}}==
<
with Ada.Strings.Fixed;
Line 289:
Test_Between ("One fish two fish red fish blue fish", First => "fish ", Last => " red");
Test_Between ("FooBarBazFooBuxQuux", First => "Foo", Last => "Foo");
end Text_Between;</
{{out}}
<pre>
Line 342:
Uses the Algol 68G specific string in string, for other compilers/interpreters, a version of string in string is here : [[ALGOL_68/prelude]].<br/>
As Algol 68 predates Unicode, the fourth example deviates from the task.
<
# some utility operators #
# returns the length of a string #
Line 412:
trace between := FALSE
END
END</
{{out}}
<pre>
Line 463:
=={{header|AppleScript}}==
<
my text_between("Hello Rosetta Code world", "Hello ", " world")
Line 485:
return return_text
end text_between
</syntaxhighlight>
=={{header|AutoHotkey}}==
<
(
Hello Rosetta Code world|Hello | world|
Line 510:
RegExMatch(text,(start="start"?"^":"\Q" start "\E") "(.*?)" (end="end"?"$":"\Q" end "\E?"),m)
return m1
}</
{{out}}
<pre>text: "Hello Rosetta Code world"
Line 550:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f TEXT_BETWEEN.AWK
BEGIN {
Line 600:
printf("Output: '%s'\n\n",str)
}
</syntaxhighlight>
{{out}}
<pre>
Line 677:
=={{header|C}}==
<syntaxhighlight lang="c">
/*
* textBetween: Gets text between two delimiters
Line 736:
return startPointer;
} // end textBetween method</
=={{header|C sharp|C#}}==
{{trans|D}}
<
namespace TextBetween {
Line 785:
}
}
}</
=={{header|C++}}==
{{trans|C#}}
<
std::ostream& operator<<(std::ostream& out, const std::string& str) {
Line 835:
return 0;
}</
{{out}}
<pre>text: 'Hello Rosetta Code world'
Line 878:
=={{header|D}}==
<
import std.stdio;
import std.string;
Line 921:
print("One fish two fish red fish blue fish", "fish ", " red");
print("FooBarBazFooBuxQuux", "Foo", "Foo");
}</
{{out}}
<pre>text: 'Hello Rosetta Code world'
Line 970:
{{libheader| System.SysUtils}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program Text_between;
Line 1,036:
Readln;
end.</
=={{header|Factor}}==
<
prettyprint.config sequences ;
IN: rosetta-code.text-between
Line 1,080:
] each ;
MAIN: text-between-demo</
{{out}}
<pre>
Line 1,132:
=={{header|FreeBASIC}}==
{{trans|VBA}}
<
Const DELIM_FINAL As String = "end"
Line 1,215:
'Resultado:
Print Salida
Sleep</
{{out}}
<pre>
Line 1,235:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,295:
fmt.Printf("Output: \"%s\"\n\n", b)
}
}</
{{out}}
Line 1,303:
=={{header|Haskell}}==
<
import Data.Bifunctor (bimap)
import Data.List (intercalate)
Line 1,371:
wrap x
| x `elem` ["start", "end"] = Left x
| otherwise = Right (pack x)</
{{Out}}
<pre>"Rosetta Code"
Line 1,382:
=={{header|J}}==
'''Solution:'''
<
text=. y
'start end'=. x
Line 1,388:
end=. text"_^:('end'&-:) end
end taketo start takeafter text
)</
'''Example Usage:'''
<
Rosetta Code</
'''Examples:'''
<
Hello Rosetta Code world
Hello Rosetta Code world
Line 1,432:
Test_output = Test_delim textBetween&.> Test_text
1 1 1 1 1 1 1 1 1</
=={{header|Java}}==
Line 1,440:
</pre>
<
public class textBetween
{
Line 1,502:
} // end class TextBetween
</syntaxhighlight>
=={{header|JavaScript}}==
===ES5===
<
function textBetween(thisText, startString, endString)
{
Line 1,545:
return newText;
} // end textBetween
</syntaxhighlight>
===ES6===
{{Trans|Haskell}}
Composed from a set of generic functions
<
'use strict';
Line 1,736:
)
);
})();</
{{Out}}
<pre>[
Line 1,750:
The implementation uses `explode` to ensure arbitrary Unicode will be handled properly.
<syntaxhighlight lang="jq">
def textbetween_strings($startdlm; $enddlm):
explode
Line 1,765:
end
| implode;
</syntaxhighlight>
=== Verification ===
<syntaxhighlight lang="text">
def testdata:
(["Hello Rosetta Code world", "Hello ", " world"],
Line 1,785:
| $in[0]
| textbetween_strings($in[1]; $in[2])
</syntaxhighlight>
===Output===
<syntaxhighlight lang="text">
"Rosetta Code"
"Hello Rosetta Code"
Line 1,797:
"two fish"
"BarBaz"
</syntaxhighlight>
=={{header|Julia}}==
{{works with|Julia|0.6}}
<
startind = startdlm != "start" ? last(search(text, startdlm)) + 1 : 1
endind = enddlm != "end" ? first(search(text, enddlm, startind)) - 1 : endof(text)
Line 1,822:
for (text, s, e) in testcases
println("\nText: ", text, "\nStart delim: ", s, "\nEnd delim: ", e, "\nOutput: ", textbetween(text, s, e))
end</
{{out}}
Line 1,872:
=={{header|Kotlin}}==
In the third example, I've assumed that the start delimiter should be "Hello " (not "Hello") to match the required output.
<
fun String.textBetween(start: String, end: String): String {
Line 1,916:
println("Output: \"$b\"\n")
}
}</
{{out}}
<pre>
Line 1,964:
Output: "BarBaz"
</pre>
=={{header|Lua}}==
French (instead of Chinese) in #4 to avoid off-topic unicode complications. Coded to satisfy this task as worded, though in actual use reader might be better served by directly applying native pattern matching facilities to specific problems.
<syntaxhighlight lang="lua">function textbetween(text, sdelim, edelim)
-- case #5 (end delimiter not present) is only problem for simplest approach, so preprocess:
if not text:find(edelim=="end" and "$" or edelim) then edelim = "end" end
-- then just:
local re = (sdelim=="start" and "^" or sdelim) .. "(.-)" .. (edelim=="end" and "$" or edelim)
return text:match(re) or ""
end
function test(text, sdelim, edelim, expected)
print(textbetween(text, sdelim, edelim) == expected)
end
test( "Hello Rosetta Code world", "Hello ", " world", "Rosetta Code" )
test( "Hello Rosetta Code world", "start", " world", "Hello Rosetta Code" )
test( "Hello Rosetta Code world", "Hello ", "end", "Rosetta Code world" )
test( "</div><div style=\"french\">bonjour</div>", "<div style=\"french\">", "</div>", "bonjour" )
test( "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">", "<text>", "<table>", "Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">" )
test( "<table style=\"myTable\"><tr><td>hello world</td></tr></table>", "<table>", "</table>", "" )
test( "The quick brown fox jumps over the lazy other fox", "quick ", " fox", "brown" )
test( "One fish two fish red fish blue fish", "fish ", " red", "two fish" )
test( "FooBarBazFooBuxQuux", "Foo", "Foo", "BarBaz" )</syntaxhighlight>
{{Out}}
All tests pass, output not given.
=={{header|Maple}}==
<
local on, off,extra:
on := piecewise(delim1="start", 1, SearchText(delim1, str)):
Line 1,974 ⟶ 2,000:
if off <> 0 then off := off+on+extra-1: end if:
return str[on+extra..off-1]:
end proc:</
{{Out|Examples}}
<pre>>textBetween("Hello Rosetta Code world", "Hello ", " world");
Line 1,996 ⟶ 2,022:
=={{header|MiniScript}}==
<
startPos = s.indexOf(startDelim) + startDelim.len
if startDelim == "start" then startPos = 0
Line 2,008 ⟶ 2,034:
print textBetween("Hello Rosetta Code world", "Hello ", "end")
print textBetween("The quick brown fox jumps over the lazy other fox", "quick ", " fox")
print textBetween("FooBarBazFooBuxQuux", "Foo", "Foo")</
{{out}}
Line 2,019 ⟶ 2,045:
=={{header|Nim}}==
<
func textBetween(text, startStr, endStr: string): string =
Line 2,061 ⟶ 2,087:
echo "End: ", endStr.quote
echo "Output: ", text.textBetween(startStr, endStr).quote
echo()</
{{out}}
Line 2,111 ⟶ 2,137:
=={{header|Objeck}}==
{{trans|Java}}
<
function : Main(args : String[]) ~ Nil {
if(args->Size() = 3) {
Line 2,152 ⟶ 2,178:
return thisText->SubString(startIndex, endIndex - startIndex);
}
}</
=={{header|Perl}}==
{{trans|Raku}}
<
sub text_between {
Line 2,197 ⟶ 2,223:
# Ignore start and end delimiter string embedded in longer words
$text = 'Soothe a guilty conscience today, string wrangling is not the best tool to use for this job.';
say '11> '. text_between($text, qr/\bthe /, qr/ to\b/);</
{{out}}
<pre>1> Rosetta Code
Line 2,212 ⟶ 2,238:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">text_between</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">end_delimiter</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">start_delimiter</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">"start"</span> <span style="color: #008080;">then</span>
Line 2,257 ⟶ 2,283:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
All tests pass, so no output.
Line 2,266 ⟶ 2,292:
</pre>
<
<?php
function text_between($string, $start, $end)
Line 2,312 ⟶ 2,338:
print_r($returnText);
?>
</syntaxhighlight>
=={{header|PowerBASIC}}==
<
#DIM ALL
#COMPILER PBCC 6
Line 2,434 ⟶ 2,460:
CON.PRINT IIF$(TextBetween(sText, StartDelim, EndDelim) = Expected, "OK", "failed")
END FUNCTION</
=={{header|Python}}==
<
#!/usr/bin/env python
from sys import argv
Line 2,483 ⟶ 2,509:
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}}==
<
(require (prefix-in 13: srfi/13))
Line 2,518 ⟶ 2,640:
(test-case "The quick brown fox jumps over the lazy other fox" "quick " " fox" "brown")
(test-case "One fish two fish red fish blue fish" "fish " " red" "two fish")
(test-case "FooBarBazFooBuxQuux" "Foo" "Foo" "BarBaz"))</
{{out}}
All tests pass, so no output.
Line 2,529 ⟶ 2,651:
This version doesn't use strings for meta indexes ('start' and 'end'), rather it accepts regex assertions which are parsed differently from strings. This allows much more robust and fine grained control over what does and doesn't match. (and allows delimiter strings of 'start' and 'end' incidentally.) See the 11th example below which will confound nearly all of the current string-only based implementations.
<syntaxhighlight lang="raku"
return $/»[0]».Str if $text ~~ m:g/ $start (.*?) $end /;
[]
Line 2,575 ⟶ 2,697:
# Ignore start and end delimiter string embedded in longer words
put '11> ', 'Soothe a guilty conscience today, string wrangling is not the best tool to use for this job.'\
.&text-between( rx/«'the '/, rx/' to'»/ );</
{{out}}
Line 2,593 ⟶ 2,715:
===version 1===
{{trans|Kotlin}}
<
/*REXX*/
Line 2,637 ⟶ 2,759:
End
o: Say arg(1) </
{{out|Output}}
<pre>Text: "Hello Rosetta Code world"
Line 2,689 ⟶ 2,811:
Also, it wasn't necessary, but I <u>assummed</u> (bad assumption?) that the <big>'''\'''</big> could be an escape character, but unless clarified,
<br>it's being treated as a commom character, REXX has no need for escape characters (within character strings).
<
call TB 'Hello Rosetta Code world', "Hello ", ' world'
call TB 'Hello Rosetta Code world', "start", ' world'
Line 2,708 ⟶ 2,830:
if E\=='end' then parse var $ $ (E) /* " " before " END. " */
say ' Output: "'$'"' /*display the extracted string to term.*/
return</
{{out|output}}
<pre>
Line 2,758 ⟶ 2,880:
=={{header|Ring}}==
<
# Project : Text between
Line 2,804 ⟶ 2,926:
next
see '"' + substr(list2str(textdel), nl, " ") +'"' + nl + nl
</syntaxhighlight>
Output:
<pre>
Line 2,825 ⟶ 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}}==
Test
<
class String
def textBetween startDelimiter, endDelimiter
Line 2,874 ⟶ 3,010:
puts returnText
</syntaxhighlight>
=={{header|Rust}}==
<syntaxhighlight lang="rust">
//Use Into<String> so input can be String, &str or anything else that implements Into<String>
fn text_between<S: Into<String>>(input: S, start: S, end: S) -> String {
Line 2,916 ⟶ 3,052:
println!("'{}'", text_between("FooBarBazFooBuxQuux", "Foo", "Foo"));
}
</syntaxhighlight>
{{out}}
<pre>
Line 2,931 ⟶ 3,067:
=={{header|Scala}}==
<
val (thisText, startDelimiter, endDelimiter) = (args(0), args(1),args(2))
Line 2,957 ⟶ 3,093:
println(textBetween(thisText, startDelimiter, endDelimiter))
}</
=={{header|Sidef}}==
Uses /^/ and /$/ as start and end delimiters. Additionally, the start and end delimiters can be regular expressions.
{{trans|Raku}}
<
beg.escape! if beg.kind_of(String)
Line 3,031 ⟶ 3,167:
assert_eq(t{:out}, r)
say "text_between(#{t{:text}.dump}, #{t{:start}.dump}, #{t{:end}.dump}) = #{r.dump}"
}</
{{out}}<pre>
text_between("Hello Rosetta Code world", "Hello ", " world") = "Rosetta Code"
Line 3,042 ⟶ 3,178:
text_between("One fish two fish red fish blue fish", "fish ", " red") = "two fish"
text_between("FooBarBazFooBuxQuux", "Foo", "Foo") = "BarBaz"</pre>
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang="snobol4">
* Program: text_between.sbl
* To run: sbl -r text_between.sbl
* Description: Get the text in a string that occurs between
* a start and end delimiter. Programs will be given a search string,
* a start delimiter string, and an end delimiter string. The delimiters
* will not be unset, and will not be the empty string.
* Comment: Tested using the Spitbol for Linux version of SNOBOL4
lf = substr(&alphabet,11,1) ;* New line or line feed
* Function text_between will return the text between start and end delimiters,
* where start can be the word 'start' for the beginning of the text,
* and end can be the word 'end' for the end of the text.
define('text_between(text,start,end)sb,eb')
:(text_between_end)
text_between
sb = (ident(start,'start') pos(0), breakx(substr(start,1,1)) start)
eb = (ident(end,'end') rem . text_between, (arb . text_between end) | (rem . text_between) )
text ? sb eb
:(return)
text_between_end
* Read text lines after the END statement
in1
line = input :f(in1end)
line ? break('|') . text '|' break('|') . start '|' rem . end
output = lf 'Text: "' text '"'
output = 'Start: "' start '"'
output = 'End: "' end '"'
text_between = text_between(text,start,end)
output = 'Output: "' text_between '"'
:(in1)
in1end
END
Hello Rosetta Code world|Hello | world
Hello Rosetta Code world|start| world
Hello Rosetta Code world|Hello |end
</div><div style=\"chinese\">你好嗎</div>|<div style=\"chinese\">|</div>
<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">|<text>|<table>
<table style=\"myTable\"><tr><td>hello world</td></tr></table>|<table>|</table>
The quick brown fox jumps over the lazy other fox|quick | fox
One fish two fish red fish blue fish|fish | red
FooBarBazFooBuxQuux|Foo|Foo
</syntaxhighlight>
{{out}}
<pre>
Text: "Hello Rosetta Code world"
Start: "Hello "
End: " world"
Output: "Rosetta Code"
Text: "Hello Rosetta Code world"
Start: "start"
End: " world"
Output: "Hello Rosetta Code"
Text: "Hello Rosetta Code world"
Start: "Hello "
End: "end"
Output: "Rosetta Code world"
Text: "</div><div style=\"chinese\">你好嗎</div>"
Start: "<div style=\"chinese\">"
End: "</div>"
Output: "你好嗎"
Text: "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">"
Start: "<text>"
End: "<table>"
Output: "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>"
Output: ""
Text: "The quick brown fox jumps over the lazy other fox"
Start: "quick "
End: " fox"
Output: "brown"
Text: "One fish two fish red fish blue fish"
Start: "fish "
End: " red"
Output: "two fish"
Text: "FooBarBazFooBuxQuux"
Start: "Foo"
End: "Foo"
Output: "BarBaz"
</pre>
=={{header|Swift}}==
<
public extension String {
Line 3,091 ⟶ 3,325:
print("End delimiter: \"\(end)\"")
print("Text between: \"\(input.textBetween(start, and: end))\"\n")
}</
{{out}}
Line 3,142 ⟶ 3,376:
=={{header|Tcl}}==
<
proc between {str start end} {
Line 3,171 ⟶ 3,405:
puts " [format %15s $t]: $v"
}
}</
{{out}}
Line 3,232 ⟶ 3,466:
The "hard" assertions when unpacking the arguments to the "text_between" function reflect the assumptions in the requirements for this problem: that null/empty arguments will never be provided. If any empty arguments are given, the interpreter running this function will exit after printing an error. If this function is invoked without a subshell, that will crash the invoking program as well. In practical use, that may not be desirable, in which case the ":?" assertions should be replaced with less harsh conditional-unpack code (e.g. <code>if [ -z "${1:-}" ]; then echo "Invalid input!" && return 127; else local var="$1"; fi</code>).
<
local search="${1:?Search text not provided}"
local start_str="${2:?Start text not provided}"
Line 3,264 ⟶ 3,498:
text_between "Hello Rosetta Code world" "Hello " " world"
text_between "Hello Rosetta Code world" "start" " world"
text_between "Hello Rosetta Code world" "Hello " "end"</
=={{header|VBA}}==
<
Private Const STRING_START As String = "Start"
Line 3,355 ⟶ 3,589:
Text_Between = Mid(T, Len(F) + InStr(T, F), InStr(InStr(T, F), T, L) - (Len(F) + InStr(T, F)))
End Select
End Function</
{{out}}
<pre>1- Rosetta Code
Line 3,372 ⟶ 3,606:
=={{header|Visual Basic}}==
{{works with|Visual Basic|6}}
<
Dim indS As Long
Dim indE As Long
Line 3,480 ⟶ 3,714:
Text = StartDelim & Expected & EndDelim & "FooBuxQuux"
Debug.Assert TextBetween(Text, StartDelim, EndDelim) = Expected
End Sub</
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function TextBetween(source As String, pre As String, suf As String) As String
Dim startIndex As Integer
Line 3,527 ⟶ 3,761:
End Sub
End Module</
{{out}}
<pre>text: 'Hello Rosetta Code world'
Line 3,573 ⟶ 3,807:
end: 'Foo'
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
}
mut s := 0
if start != "start" {
s = str.index(start) or {-1}
}
if s == -1 {
return ""
}
mut si := 0
if start != "start" {
si = s + start.len
}
mut e := str.len
if end != "end" {
e = str[si..].index(end) or {-1}
if e == -1 {
return str[si..]
}
e += si
}
return str[si..e]
}
fn main() {
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, text in texts {
println("Text: \"$text\"")
println("Start delimiter: \"${starts[i]}\"")
println("End delimiter: \"${ends[i]}\"")
b := text_between(text, starts[i], ends[i])
println("Output: \"$b\"\n")
}
}</syntaxhighlight>
{{out}}
<pre>
Text: "Hello Rosetta Code world"
Start delimiter: "Hello "
End delimiter: " world"
Output: "Rosetta Code"
Text: "Hello Rosetta Code world"
Start delimiter: "start"
End delimiter: " world"
Output: "Hello Rosetta Code"
Text: "Hello Rosetta Code world"
Start delimiter: "Hello "
End delimiter: "end"
Output: "Rosetta Code world"
Text: "</div><div style="chinese">你好嗎</div>"
Start delimiter: "<div style="chinese">"
End delimiter: "</div>"
Output: "你好嗎"
Text: "<text>Hello <span>Rosetta Code</span> world</text><table style="myTable">"
Start delimiter: "<text>"
End delimiter: "<table>"
Output: "Hello <span>Rosetta Code</span> world</text><table style="myTable">"
Text: "<table style="myTable"><tr><td>hello world</td></tr></table>"
Start delimiter: "<table>"
End delimiter: "</table>"
Output: ""
Text: "The quick brown fox jumps over the lazy other fox"
Start delimiter: "quick "
End delimiter: " fox"
Output: "brown"
Text: "One fish two fish red fish blue fish"
Start delimiter: "fish "
End delimiter: " red"
Output: "two fish"
Text: "FooBarBazFooBuxQuux"
Start delimiter: "Foo"
End delimiter: "Foo"
Output: "BarBaz"
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var textBetween = Fn.new { |str, start, end|
Line 3,622 ⟶ 3,961:
Fmt.print("Output: $q\n", b)
i = i + 1
}</
{{out}}
Line 3,670 ⟶ 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>
=={{header|Yabasic}}==
{{trans|Wren}}
<
// by Galileo, 04/2022
Line 3,711 ⟶ 4,109:
print "End delimiter: ", end$
print "Output: ", textBetween$(text$, start$, end$), "\n"
loop</
{{out}}
<pre>Text: Hello Rosetta Code world
Line 3,761 ⟶ 4,159:
=={{header|zkl}}==
<
s = (if((s:=text.find(start))==Void) 0 else s + start.len());
e = (if((e:=text.find(end,s))==Void) text.len() else e);
Line 3,768 ⟶ 4,166:
getText("Hello Rosetta Code world","Hello "," world").println();
getText("Hello Rosetta Code world","start", " world").println();
getText("Hello Rosetta Code world","Hello", "end" ).println();</
{{out}}
<pre>
|