Standard Library/strings

From J Wiki
Jump to navigation Jump to search
Overview | android | break | colib | compare | coutil | dates | debug | dir | dll | files | jade | jal | stdlib | strings | sysenv | text | unxlib

This script defines string utilities and is included in the J standard library. Definitions are loaded into the z locale.


C charsub, chopstring, cut, cuts
D deb, delstring, dlb, dltb, dltbs, dquote, dropafter, dropto, dtb, dtbs
F fstringreplace
J joinstring
L ljust
Q quote
R rjust, rplc
S splitnostring, splitstring, ss, stringreplace
T takeafter, taketo
U undquote

cut v cut text, by default on blanks
deb v delete extra blanks
delstring v delete occurrences of x from y
dlb v delete leading blanks
dltb v delete leading and trailing blanks
dropafter v drop after x in y
dropto v drop to x in y
dtb v delete trailing blanks
joinstring v join boxed list y with x; see splitstring
ljust v left justify
quote v quote text
rjust v right justify
ss v string search
takeafter v take after x in y
taketo v take to x in y

charsub (verb)

Character substitution.


characterpairs charsub string


   '-_$ ' charsub '$123 -456 -789'
123 _456 _789


Use rplc for arbitrary string replacement.

chopstring (verb)

Chop delimited string to list of boxed strings.


[fd[;sd0[,sd1]]] chopstring string

returns: list of boxed literals
 y is: delimited string
 x is: a literal or 1 or 2-item boxed list of optional delimiters.
     0{:: single literal field delimiter (fd). Defaults to ' '
 (1;0){:: (start) string delimiter (sd0). Defaults to "
 (1;1){:: end string delimiter (sd1). Defaults to "

Consecutive field delimiters indicate empty field. Field delimiters may occur within a field if the field is enclosed by string delimiters.


   ('|';'<>') chopstring '<hello|world>|4|84.3'

cuts (conjunction)

This builds verbs to cut strings at given text and apply verbs to the pieces.


string (verb cuts n) text

 n=_1 up to but not including string
 n= 1 up to and including string
 n=_2 after but not including string
 n= 2 after and including string

dltbs (verb)

Delete multiple leading and trailing blanks. Text is delimited by characters in x with default LF


   < 'A' dltbs ' A abc  def  Ars  A  x y  z  '
│Aabc  defArsAx y  z│

dquote (verb)

Double quote text.


   dquote 'Pete"s Place'
"Pete""s Place"

dtbs (verb)

Delete multiple trailing blanks in text. Text is delimited by characters in x with default CRLF


   < 'A' dtbs ' A abc  def  Ars  A  x y  z  '
│A abc  defArsA  x y  z│

fstringreplace (verb)

Replace strings in file


(old;new) fstringreplace file

rplc (verb)

This is stringreplace but with arguments reversed.


   'hello' rplc 'e';'a';'o';'owed'

splitnostring (verb)

Split y by non-overlapping substrings x This is a non-overlapping variant of E.

splitstring (verb)

Split y by substring x. see joinstring.

stringreplace (verb)

Replace characters in text string.


oldnew stringreplace text

oldnew is a 2-column boxed matrix of old ,. new or a vector of same

stringreplace priority is the same order as oldnew


    ('aba';'XYZT';'ba';'+') stringreplace 'ababa'

    ('ba';'+';'aba';'XYZT') stringreplace 'ababa'

undquote (verb)

Undo double quote text.


   undquote '"Pete""s Place"'
Pete"s Place