Substring: Difference between revisions
m
→{{header|Phix}}: use pygments
m (→{{header|Phix}}: use pygments) |
|||
Line 4,355:
=={{header|Phix}}==
{{libheader|Phix/basics}}
<!--(phixonline)-->
--(2) starting from n characters in, up to the end of the string;
--(5) starting from a known substring within the string and of m length.
constant sentence = "the last thing the man said was the",
n = 10, m = 5
integer k, l
l = n+m-1
if l<=length(sentence) then
?sentence[n..l] -- (1)
end if
if n<=length(sentence) then
?sentence[n..-1] -- (2) or [n..$]
end if
if length(sentence)>0 then
?sentence[1..-2] -- (3) or [1..$-1]
end if
k = find('m',sentence)
l = k+m-1
if l<=length(sentence) then
?sentence[k..l] -- (4)
end if
k = match("aid",sentence)
l = k+m-1
if l<=length(sentence) then
?sentence[k..l] -- (5)
end if
{{out}}
<pre>
Line 4,395 ⟶ 4,396:
</pre>
Alternative version with no error handling, for those in a hurry (same ouput):
?sentence[n..n+m-1]
?sentence[n..-1]
?sentence[1..-2]
?(sentence[find('m',sentence)..$])[1..m]
?(sentence[match("aid",sentence)..$])[1..m]
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.
|