String

Text operations on data values: length, case, trim, split, replace, slice, pad, repeat, search, regular expressions, format and JSON.

  • Data
  • Since 0.2.0
  • string
  • text
  • regex
  • split
  • join
  • replace
  • format
  • json
  • data

Open in TuneToys

String as the Patch draws it, at default values: inlets on the left, outlets on the right.

String transforms text: change case, cut it into a list, replace parts of it, search it or turn it into numbers and JSON.

What it does#

The text input is read as text (a number becomes its digits, a list becomes its JSON). Operation chooses what happens to it. Some operations need more: b is the search text, separator or padding, and c is the replacement. When the b or c input is not connected, String uses the node's B or C text field instead.

OperationResult
Lengthnumber of characters
Concatenatetext followed by B
Upper case, Lower case, Trim, Reversethe changed text
Splita list of the pieces between each B; empty B splits into characters
Replace, Replace allthe first, or every, B replaced by C
SliceCount characters from Start; Count 0 means to the end
Pad start, Pad endtext padded with B (a space by default) to Count characters
Repeattext repeated Count times
Character atthe character at Start, or empty text
Index ofposition of B at or after Start, or -1
Matcha list of the match and its groups, or no value when nothing matches
Formateach {} in the text replaced by the next element of B; {0} picks one
To number, Parse JSON, To JSONconversions; text that does not convert sends no value

Negative Start counts from the end. With Regular expression off, B is searched for literally; turn it on to read B as a pattern for Split, Replace, Replace all, Index of and Match. An invalid pattern sends no value and the inspector shows the error.

When to use it#

Use it to clean up typed or imported text, to split a list of names or numbers written on one line, to build labels from values with Format, or to pull numbers out of text with Match and To number. To join a list back into text, use List Join.

Ports#

PortDirectionSignalLanes
textInputData (any)1
bInputData (any)1
cInputData (any)1
valueOutputData (any)1

Parameters#

ParameterLabelRangeDefaultUnitScaleNotes
opOperation0 to 190linearText operation; b and c are its arguments. Choices: 0 Length, 1 Concatenate, 2 Upper case, 3 Lower case, 4 Trim, 5 Split, 6 Replace, 7 Replace all, 8 Slice, 9 Pad start, 10 Pad end, 11 Repeat, 12 Reverse, 13 Character at, 14 Index of, 15 Match, 16 Format, 17 To number, 18 Parse JSON, 19 To JSON.
startStart-4096 to 40960linearStart index; negative counts from the end. Shown when op is 8 or 13 or 14.
countCount0 to 40961linearLength, pad width or repeat count. Shown when op is 8 or 9 or 10 or 11.
regexRegular expression0 to 10linearTreat b as a regular expression. Choices: 0 Off, 1 On. Shown when op is 5 or 6 or 7 or 14 or 15.
caseCase0 to 10linearWhether matching respects letter case. Choices: 0 Sensitive, 1 Insensitive. Shown when op is 5 or 6 or 7 or 14 or 15.
  • b: text.
  • c: text.

Operation (op)#

The text operation, a discrete choice. See the table above for what each one does and which fields it uses.

Start (start)#

A character position from -4 096 to 4 096. 0 is the first character and -1 the last. Used by Slice, Character at and Index of.

Count (count)#

A number of characters or repeats, from 0 to 4 096. Slice takes Count characters (0 takes the rest), Pad start and Pad end pad to Count characters, Repeat repeats Count times.

Regular expression (regex)#

Off or On, a discrete choice. Off reads B as plain text, so . is a full stop. On reads B as a pattern, so [0-9]+ means any run of digits and Match returns capture groups.

Case (case)#

Sensitive or Insensitive, a discrete choice. Insensitive makes Index of ignore case and adds the i flag to regular expressions.

Tips#

  • To split on commas with or without spaces, turn Regular expression on and set B to \s*,\s*.
  • To replace every number in a line, use Replace all with Regular expression on, B set to [0-9]+ and C to the replacement.
  • Format with B connected to a list builds labels: "{} of {}" with [3, 8] gives "3 of 8".
  • Text fields hold up to 1 024 characters; longer results are cut at 65 536 characters and flagged.

See also#

Value, List, Compare.

Help patches

Each help patch is an ordinary Toy that teaches one idea. The diagram shows the Patch; the preview shows its Frame. Open it in TuneToys to play and edit it.

1. Split and join text

beginner Teaches: op, regex

A comma-separated line split into a list and joined again with a new separator.

Patch: Oscillator audio output connected to Patch outputs audio input. Value value output connected to String text input. String value output connected to List list input.
Connections as text
  • Oscillator audio output connected to Patch outputs audio input
  • Value value output connected to String text input
  • String value output connected to List list input
  • Split cuts the text at every "," (the B field) and sends a list. List Join glues it back with " / ".
  • The spaces stay. Set B to \s*,\s* and turn Regular expression on to split and trim in one step.
  • Data cables carry values, not sound. Select a data node to see its newest value in the inspector; the tone only keeps the Toy audible.
Frame preview: Split and join textFrame “Split and join text” with 2 elements: text heading, text hint.SPLIT AND JOIN TEXTData nodes are set in the inspector: select one on the Patch to change Operatio…
Frame preview (static). Controls show their default values.

Open in TuneToys

2. Replace with a pattern

intermediate Teaches: op, regex, case

Every number in a line replaced using a regular expression.

Patch: Oscillator audio output connected to Patch outputs audio input. Value value output connected to String text input.
Connections as text
  • Oscillator audio output connected to Patch outputs audio input
  • Value value output connected to String text input
  • B is the pattern [0-9]+ and C is the replacement #. Replace all gives "Tempo # BPM, swing # bpm".
  • Replace changes only the first match. Turn Regular expression off and B is searched for literally.
  • Set B to bpm: Case Sensitive replaces one, Insensitive replaces both.
  • Data cables carry values, not sound. Select a data node to see its newest value in the inspector; the tone only keeps the Toy audible.
Frame preview: Replace with a patternFrame “Replace with a pattern” with 2 elements: text heading, text hint.REPLACE WITH A PATTERNData nodes are set in the inspector: select one on the Patch to change Operatio…
Frame preview (static). Controls show their default values.

Open in TuneToys

  • Value A literal source, the data domain's number box and message box.
  • List List operations on data values: get, push, insert, remove, slice, sort, unique, flatten, join, range, sum, minimum, maximum, mean, shuffle and more.
  • Compare Compare two data values as numbers, text or lists and send true or false: ordering, equality, contains, starts or ends with, pattern match, empty, in list.

Edit this page on GitHub

esc

↑ ↓ move↩ open⌘↩ new tabesc close