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.

Definitions

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.

Syntax:

characterpairs charsub string

Example:

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

Note

Use rplc for arbitrary string replacement.

chopstring (verb)

Chop delimited string to list of boxed strings.

Syntax:

[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.

Example:

   ('|';'<>') chopstring '<hello|world>|4|84.3'
┌───────────┬─┬────┐
│hello|world│4│84.3│
└───────────┴─┴────┘

cuts (conjunction)

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

Syntax:

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

Example:

   < 'A' dltbs ' A abc  def  Ars  A  x y  z  '
┌───────────────────┐
│Aabc  defArsAx y  z│
└───────────────────┘

dquote (verb)

Double quote text.

Example:

   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

Example:

   < 'A' dtbs ' A abc  def  Ars  A  x y  z  '
┌──────────────────────┐
│A abc  defArsA  x y  z│
└──────────────────────┘

fstringreplace (verb)

Replace strings in file

Syntax:

(old;new) fstringreplace file

rplc (verb)

This is stringreplace but with arguments reversed.

Example:

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

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.

Syntax:

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

Example:

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

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

undquote (verb)

Undo double quote text.

Example:

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