/* Substring (Rosetta code) in Picat. http://rosettacode.org/wiki/Substring """ In this task display a substring: - starting from n characters in and of m length; - starting from n characters in, up to the end of the string; - whole string minus last character; - starting from a known character within the string and of m length; - starting from a known substring within the string and of m length. If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point, whether in the Basic Multilingual Plane or above it. The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => S = "Picat is fun", N = 3, M = 4, C = 'i', % must be a char SS = "is", test(S,N,M,C,SS), nl. test(S,N,M,C,SS) => println($test(S,N,M,C,SS)), % - starting from n characters in and of m length; println(1=slice(S,N,N+M)), println(1=S[N..N+M]), % - starting from n characters in, up to the end of the string; println(2=S.slice(N)), % - whole string minus last character; println(3=but_last(S)), println(3=S[1..S.len-1]), % - starting from a known character within the string and of m length; println(4=substring4(S,C)), % - starting from a known substring within the string and of m length. println(5=substring5(S,SS,M)), nl. but_last(S) = slice(S,1,S.length-1). substring4(S,C) = slice(S,S.find_first_of(C)). substring5(S,SS,M) = slice(S,Start,Start+M) => % find is non-deterministic, hence the once/1 once(find(S,SS,Start,_End)).