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
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.
| Operation | Result |
|---|---|
| Length | number of characters |
| Concatenate | text followed by B |
| Upper case, Lower case, Trim, Reverse | the changed text |
| Split | a list of the pieces between each B; empty B splits into characters |
| Replace, Replace all | the first, or every, B replaced by C |
| Slice | Count characters from Start; Count 0 means to the end |
| Pad start, Pad end | text padded with B (a space by default) to Count characters |
| Repeat | text repeated Count times |
| Character at | the character at Start, or empty text |
| Index of | position of B at or after Start, or -1 |
| Match | a list of the match and its groups, or no value when nothing matches |
| Format | each {} in the text replaced by the next element of B; {0} picks one |
| To number, Parse JSON, To JSON | conversions; 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#
| Port | Direction | Signal | Lanes |
|---|---|---|---|
text | Input | Data (any) | 1 |
b | Input | Data (any) | 1 |
c | Input | Data (any) | 1 |
value | Output | Data (any) | 1 |
Parameters#
| Parameter | Label | Range | Default | Unit | Scale | Notes |
|---|---|---|---|---|---|---|
op | Operation | 0 to 19 | 0 | linear | Text 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. | |
start | Start | -4096 to 4096 | 0 | linear | Start index; negative counts from the end. Shown when op is 8 or 13 or 14. | |
count | Count | 0 to 4096 | 1 | linear | Length, pad width or repeat count. Shown when op is 8 or 9 or 10 or 11. | |
regex | Regular expression | 0 to 1 | 0 | linear | Treat b as a regular expression. Choices: 0 Off, 1 On. Shown when op is 5 or 6 or 7 or 14 or 15. | |
case | Case | 0 to 1 | 0 | linear | Whether 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#
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.
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.
2. Replace with a pattern
intermediate Teaches: op, regex, case
Every number in a line replaced using a regular expression.
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.
Related nodes
- 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.