Skip to content

ss, ssr

String search and replace

ss

String search

x ss y     ss[x;y]

Where

  • x is a string
  • y is a pattern as a char atom or string (ss doesn't support * as a wildcard)

returns a long vector of the positions within x of substrings that match pattern y.

q)"We the people of the United States" ss "the"
3 17

q)s:"toronto ontario"
q)s ss "ont"
3 8
q)s ss "[ir]o"
2 13
q)s ss "t?r"
0 10

Matches do not overlap: after a match, the search resumes at the end of the matched substring.

q)"aaaa" ss "aa"
0 2

ssr

String search and replace

ssr[x;y;z]

Where

  • x is a string
  • y is a pattern as a char atom or string (ssr doesn't support * as a wildcard)
  • z is a string or a function

returns x with each substring matching y replaced by:

  • z if z is a string
  • z[Y] if z is a function, where Y is the matched substring
q)s:"toronto ontario"
q)ssr[s;"ont";"x"]      / replace "ont" by "x"
"torxo xario"
q)ssr[s;"t?r";upper]    / replace matches by their uppercase
"TORonto onTARio"

like

Wildcard patterns in q

Strings