56 lines
1.1 KiB
Plaintext
56 lines
1.1 KiB
Plaintext
main => % - String assignment
|
|
S1 = "binary_string",
|
|
println(s1=S1),
|
|
|
|
% Picat has re-assignments (:=/2) as well,
|
|
S1 := "another string",
|
|
println(s1=S1),
|
|
|
|
% - String comparison
|
|
if S1 == "another string" then
|
|
println(same)
|
|
else
|
|
println(not_same)
|
|
end,
|
|
|
|
% - String cloning and copying
|
|
S2 = S1,
|
|
println(s2=S2),
|
|
|
|
S3 = copy_term(S1), % for strings it's the same as =/2
|
|
println(s3=S3),
|
|
|
|
% - Check if a string is empty
|
|
if S3 == "" then
|
|
println(is_empty)
|
|
else
|
|
println(not_empty)
|
|
end,
|
|
|
|
% - Append a byte to a string
|
|
S3 := S3 ++ "s",
|
|
println(s3=S3),
|
|
|
|
% - Extract a substring from a string
|
|
println(substring=S3[5..7]),
|
|
println(slice=slice(S1,5,7)),
|
|
println(slice=slice(S1,5)),
|
|
|
|
% - Replace every occurrence of a byte (or a string) in a string with another string
|
|
S4 = replace(S3,'s','x'),
|
|
println(s4=S4),
|
|
|
|
% - Join strings
|
|
S5 = S1 ++ " " ++ S2,
|
|
println(s5=S5),
|
|
|
|
% using append/4
|
|
append(S1," ", S2,S6),
|
|
println(s6=S6),
|
|
|
|
% find positions of substrings
|
|
println(find=findall([From,To],find(S5,"str",From,To))),
|
|
|
|
% split a string
|
|
println(split=split(S1," st"))
|