String splitting is the process of breaking up a text string in a systematic way so that the individual parts of the text can be processed. When wanting to split a delimited string to get a list of items (such as brands), the quickest method is to use the List of Items automation. This is typically used when coding comma-delimited spontaneous awareness data.
In other scenarios where you want to do something more custom, you may need to use JavaScript to split up the strings. This article describes how to go from a single variable where commas separate the 1st, 2nd, and 3rd mentions, and so on ...
To storing each mention in a separate variable:
Requirements
- A data set loaded into Displayr that contains a text variable where multiple responses are stored in a single variable as comma-separated text values.
Method
In Displayr, to split the text variable requires adding new variables that will appear alongside the original text variable in the Data Sources tree, similar to below:
To do so, follow these steps:
- Hover over any variable in your Data Sources and click Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Text. A new variable will be created in the Data Sources tree.
- Next, copy and paste the code below into the JavaScript code editor:
// Define the text variable to split
var string = awareness
// Define the delimiter
var split_string = string.split(",");
// Extract the first string of before the delimiter (first string field = 0, second string field = 1 etc)
var result = split_string[0]
// Return the result
result - Click Calculate.
- With this new variable selected, go to the object inspector, and under General > General give the variable a Label and a Name.
This code does the following things:
The split() method is used to split a string into an array of substrings and it returns a new array.
Tip: If an empty string ("") is used as the separator, the string is split between each character.
Note: The split() method does not change the original string.
The syntax:
string.split(separator, limit)
OPTIONAL: The separator specifies the character, or the regular expression, to use for splitting the string, as in the example above, ",". If omitted, the entire string will be returned (an array with only one item).
OPTIONAL: limit is an integer that specifies the number of splits, items after the split limit will not be included in the array.
Next
Finding the Best Text Analysis for your Data