This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,18 @@
text s;
data b, d;
s = "The quick brown fox jumps over the lazy dog.";
o_text(cut(s, 4, 15));
o_newline();
o_text(cut(s, 4, length(s)));
o_newline();
o_text(delete(s, -1));
o_newline();
o_text(cut(s, index(s, 'q'), 5));
o_newline();
b_cast(b, s);
b_cast(d, "brown");
o_text(cut(s, b_find(b, d), 15));
o_newline();

View file

@ -0,0 +1,25 @@
MODULE Substrings;
IMPORT StdLog,Strings;
PROCEDURE Do*;
CONST
aStr = "abcdefghijklmnopqrstuvwxyz";
VAR
str: ARRAY 128 OF CHAR;
pos: INTEGER;
BEGIN
Strings.Extract(aStr,3,10,str);
StdLog.String("from 3, 10 characters:> ");StdLog.String(str);StdLog.Ln;
Strings.Extract(aStr,3,LEN(aStr) - 3,str);
StdLog.String("from 3, until the end:> ");StdLog.String(str);StdLog.Ln;
Strings.Extract(aStr,0,LEN(aStr) - 1,str);
StdLog.String("whole string but last:> ");StdLog.String(str);StdLog.Ln;
Strings.Find(aStr,'d',0,pos);
Strings.Extract(aStr,pos + 1,10,str);
StdLog.String("from 'd', 10 characters:> ");StdLog.String(str);StdLog.Ln;
Strings.Find(aStr,"de",0,pos);
Strings.Extract(aStr,pos + LEN("de"),10,str);
StdLog.String("from 'de', 10 characters:> ");StdLog.String(str);StdLog.Ln;
END Do;
END Substrings.

View file

@ -0,0 +1,21 @@
:- object(substring).
:- public(test/5).
test(String, N, M, Character, Substring) :-
sub_atom(String, N, M, _, Substring1),
write(Substring1), nl,
sub_atom(String, N, _, 0, Substring2),
write(Substring2), nl,
sub_atom(String, 0, _, 1, Substring3),
write(Substring3), nl,
% there can be multiple occurences of the character
once(sub_atom(String, Before4, 1, _, Character)),
sub_atom(String, Before4, M, _, Substring4),
write(Substring4), nl,
% there can be multiple occurences of the substring
once(sub_atom(String, Before5, _, _, Substring)),
sub_atom(String, Before5, M, _, Substring5),
write(Substring5), nl.
:- end_object.

View file

@ -0,0 +1,7 @@
| ?- ?- substring::test('abcdefgh', 2, 3, 'b', 'bc').
cde
cdefgh
abcdefg
bcd
bcd
yes

View file

@ -3,5 +3,6 @@ n=4; m=3;
u=substr(s,n,m);
u=substr(s,n);
u=substr(s,1,length(s)-1);
u=substr(s,index(s,'def',m);
u=substr(s,index(s,'g',m);
u=left(s,length(s)-1);
u=substr(s,1,length(s)-1);
u=substr(s,index(s,'g'),m);

View file

@ -1,20 +1,37 @@
/*REXX program demonstrates various ways to extract substrings from a string of characters. */
s='abcdefghijk'; n=4; m=3 /*define come REXX constants (string, index, length of string).*/
say 'original string:' s /* [↑] M can be zero (which indicates a null string). */
s='abcdefghijk'; n=4; m=3 /*define some REXX constants (string, index, length of string).*/
say 'original string='s /* [↑] M can be zero (which indicates a null string). */
say '1'
u=substr(s,n,m) /*starting from N characters in and of M length. */
say u
parse var s =(n) a +(m) /*another way of doing the above by using the PARSE instruction*/
say a
say '2'
u=substr(s,n) /*starting from N characters in, up to the end-of-string. */
say u
parse var s =(n) a /*another way of doing the above by using the PARSE instruction*/
say a
say '3'
u=substr(s,1,length(s)-1) /*OK: the whole string except the last character. */
u=substr(s,1,max(0,length(s)-1)) /*better: this version handles the case of a null string. */
say u
v=substr(s,1,max(0,length(s)-1)) /*better: this version handles the case of a null string. */
say v
L=length(s) - 1
parse var s a +(L) /*another way of doing the above by using the PARSE instruction*/
say a
say '4'
u=substr(s,pos('def',s),m) /*starting from a known char within the string & of M length.*/
u=substr(s,pos('g',s),m) /*starting from a known char within the string & of M length.*/
say u
parse var s 'g' a +(m) /*another way of doing the above by using the PARSE instruction*/
say a
say '5'
u=substr(s,pos('g',s),m) /*starting from a known substr within the string & of M length.*/
u=substr(s,pos('def',s),m) /*starting from a known substr within the string & of M length.*/
say u
parse var s 'def' a +(m) /*another way of doing the above by using the PARSE instruction*/
say a
/*stick a fork in it sir, we're all done and Bob's your uncle. */

View file

@ -1,9 +1,22 @@
val str = "The good life is one inspired by love and guided by knowledge."
val n = 21
val m = 16
object Substring {
// Ruler 1 2 3 4 5 6
// 012345678901234567890123456789012345678901234567890123456789012
val str = "The good life is one inspired by love and guided by knowledge."
val (n, m) = (21, 16) // An one-liner to set n = 21, m = 16
println(str.slice(n, n+m))
println(str.slice(n, str.length))
println(str.slice(0, str.length-1))
println(str.slice(str.indexOf('l'), str.indexOf('l')+m))
println(str.slice(str.indexOf("good"), str.indexOf("good")+m))
// Starting from n characters in and of m length
assert("inspired by love" == str.slice(n, n + m))
// Starting from n characters in, up to the end of the string
assert("inspired by love and guided by knowledge." == str.drop(n))
// Whole string minus last character
assert("The good life is one inspired by love and guided by knowledge" == str.init)
// Starting from a known character within the string and of m length
assert("life is one insp" == { val i = str.indexOf('l'); str.slice(i, i + m) })
// Alternatively
assert("life is one insp" == str.drop(str.indexOf('l')).take(m))
// Starting from a known substring within the string and of m length
assert("good life is one" == { val i = str.indexOf("good"); str.slice(i, i + m) })
// Alternatively
assert("good life is one" == str.drop(str.indexOf("good")).take(m))
}