Manipulating Text with JavaScript

From Q
Jump to navigation Jump to search

This page contains a list of functions that are designed to be used for manipulation text in variables. See JavaScript Text Analysis Functions for functions for interrogating text in variable labels and value labels.

The examples below assume a text variable (a string) containing the first eight letters of the alphabet, where every second letter is a capital: aBcDeFgH. In the examples below, we show both how to manipulate this as a string of text and how to manipulate a variable (constructed via Var s = "aBcDeFgH (see creating variables in the Expression).

Common ways to manipulate text

Expression using string Expression using variable Result Explanation
"aBcDeFgH"+"aBcDeFgH" s + s aBcDeFgHaBcDeFgH Concatenates (i.e., adds) the two strings together, producing:

aBcDeFgHaBcDeFgH.

"aBcDeFgH".length s.length 8 Returns the number of characters in the string
"aBcDeFgH".substring(2) s.substring(2) cDeFgH Returns all but the first two characters of the string)
"aBcDeFgH".substring(2,4) s.substring(2,4) cD Returns the first four character, excluding the first two characters)
"aBcDeFgH".toUpperCase() s.toUpperCase() ABCDEFGH Converts characters to capitals (uppercase)
"aBcDeFgH".toLowerCase () s.toLowerCase () abcdefgh Converts characters to lowercase
"aBcDeFgH".indexOf("c") s.indexOf("c") 2 The number of characters that precede the beginning of the search term (i.e., the bit in brackets)
"aBcDeFgH".lastIndexOf("c") s.lastIndexOf ("c") 2 The number of characters that precede the beginning of the last instance of the search term (i.e., the bit in brackets)
"aBcDeFgH".slice(-2) s.slice(-2) gH Returns the final two characters
"aBcDeFgH".search("B") x.search("B") 1 Returns the position where the search term appears in the string of text (where the initial position is 0, the second character is 1, etc.)
"aabbBBBB".replace("BB","CC") x.replace("BB","CC") aabbCCBB Replaces the first instance of a string with another string.
"aabbBBBB".replace(/BB/g,"CC") x.replace(/BB/g,"CC") aabbCCCC Replaces all instances of a string with another string (g stands for global).
"aabbBBBB".replace(/BB/gi,"CC") x.replace(/BB/gi,"CC") aaCCCCCC Replaces all instances of a string with another string, ignoring case.

Example

Consider the situation where a data file has been set up containing a Categorical Variable where the Values and Labels are as shown in the first two columns, and the desire is to have a variable containing values as shown in the third column.

Value Label Desired Values (as a Text Variable)
1 February 2006 1 Feb 2006
2 March 2006 1 Mar 2006
3 April 2006 1 Apr 2006
4 May 20065 1 May 2006
13 February 2007 1 Feb 2007

Assuming that the variable is called MTH, the following expression would perform the desired conversion:

var monthYear = Q.Label(MTH);    //turning label into text variable
var yearStart = monthYear.indexOf("20");    // century
"1 " + monthYear.substring(0,3) + " " + monthYear.substring(yearStart);

More elegantly, we could have written:

var monthYear = Q.Label(MTH);    //turning label into text variable
"1 " + monthYear.substring(0,3) + " " + monthYear.slice(-4);

And, it could have been written parsimoniously as:

"1 " + Q.Label(MTH).substring(0,3) + " " + Q.Label(MTH).slice(-4);

Of course, using Q’s inbuilt functions for converting data to the Variable Type of Date is the fastest way of dealing with such data - see How to Create a Date Variable (i.e., these examples are for illustrative purposes only).

The role of the + operator

If you create an expression which has an arithmetic meaning, the arithmetic will be conducted. For example, ID + ID would create a new Text Variable where each observation’s value is double their value on the ID variable. For example, IDs from 1 to 10 would appear as:

2
4
6
8
10
12
14
16
18
20

By contrast, by adding a space between the variables (ID + " " + ID), Q instead treats the input variables as strings:

1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10

See also

Creating Text Variables