\\ Replaces every letter of x by the corresponding letter of y. t_replace (a,x,y) = {my (i); if (type(a)!="t_STR", return(a)); y=Vec(y); a=Vec(a); t_word(apply(s->if (i=t_pos(s,x),y[i],s),a),,"")} \\ Concat of k copies of string a. t_repstr (a=" ",k) = if (k, concat(vector(k,i,a)), "") \\ String splitting. The letters in Sep are used as separators. \\ If separators appear at the beginning or end of a, empty \\ words are attached. t_split (a,Sep=" \n") = {my (n,P,B,u); n=#a; P=t_posall(Sep,a); a=Vec(a); B=t_progressiveblocks(setminus([1..n],P)); u=[t_strip(concat(a[vecmin(b)..vecmax(b)])) | b<-B]; if (t_pos(1,P), u=concat([""],u)); if (t_pos(n,P), u=concat(u,[""])); u} \\ 1st part of split, if it exists. t_split_1 (a,Sep) = {my (u); u=t_split(a,Sep); if (#u, u[1],"")} \\ 2nd part of split, if it exists. t_split_2 (a,Sep) = {my (u); u=t_split(a,Sep); if (#u>1, u[2], a)} \\ Extracts [a,b] from "a=b"; if no "=", then b=def. t_splitdef (a,def=1) = {my (u); if (!#t_posall("=",a), a=Strexpand(a,"=",def)); u=t_split(a,"="); apply(t_strip,u[1..2])} \\ Eliminates letters of Sep on both sides of string a. t_strip (a,Sep=" \n") = {my (i,j,n); a=Vec(a); i=0; j=0; n=#a; Sep=Vec(Sep); for (k=1,n,if (!t_pos(a[k],Sep),break, i++)); for (k=1,n-i,if(!t_pos(a[n-k+1],Sep),break, j++)); if (i+1>n-j, "", t_word(a[i+1..n-j],,""))} t_word (u,format="%s",sep=" ",rowsep="\n") = {my (typ,a,b,J); typ=type(u); if (typ=="t_VEC" || typ=="t_LIST",return (t_wordchain(u,format,sep))); if (typ=="t_MAT",n=matsize(u)[1]; J=vector(n,i,i); a=apply(i->t_wordchain(u[i,],format,sep),J); b=t_wordchain(a,,rowsep); return(b)); Strprintf(format,u)} \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\ Auxiliary, not used normally. A simpler function is Strexpand. \\ If g>0, groups of length g separated by sep are formed. t_wordchain (u,format="%s",sep=" ",g=0) = {my (n,t); n=#u; if (!n,return("")); t=Strprintf(format,u[1]); if(g,gsep=sep;sep=""); for (i=2,n,if(g&&((i-1)%g==0),t=concat(t,gsep)); t=concat(t,concat(sep,Strprintf(format,u[i])))); t}