Apply

How to Find the Index of a Character within a String (INDEXOF)

Using Advanced Expression Piping within your forms you can create mathematical formulas in order to calculate a value. Use INDEXOF to reference the location of a character/value within a string.

The INDEXOF function allows for two string values to be used and returns the index of the second value within the first value.

  • The indexing of the values starts at 0
  • In the case where the value is not found the response will return –1

If "example" is the first value and we're looking for ‘x’ within it, the expression would return 1. e=(0), x=(1), a=(3), etc.

The setup and formatting of the INDEXOF expression is imperative in preventing an error message from appearing on your form where the result of your expression would normally appear. The first line of the INDEXOF expression should be set up as follows:

{{ INDEXOF(variable,‘value’) }}

TIP! The reference to variable in the expression above can be either the identifier of a question or the index of a field within a question.

  • Each expression is always contained within a pair of double "curly" brackets {{  }}
  • There should be a space between the first instance of INDEXOF and the last bracket
  • Variables and values you are using are case sensitive
  • Always close each INDEXOF operation with brackets ( )

To reference our previous example where we are locating the index of a value within the word "example", our expression could be:

{{ INDEXOF(example,'x') }} = 1
{{ INDEXOF(example,'e') }} = 0
{{ INDEXOF(example,'s') }} = -1

More commonly, the index of a value can be pulled from the response of a text response question or from a response of a specific cell within a grid. 

INDEXOF a Text Response Question{{ INDEXOF(q1,'a') }}Will tell us the index of the value 'a' within question 1.
NOTE: In this example, q1 has been used as the identifier of question 1.
INDEXOF a single cell{{ INDEXOF(q2[0],'@') }}Based on Grid Indexing, this will tell us the index of the value '@' within the first cell of question 2. 

Consider a case where it is required to know the number of characters before the @ symbol within an email response field. The question where the email is requested from the applicant falls within a Single Column question with the identifier about and is the fifth subquestion within the grid question.

Identifier:about
Question reference:about.4
Expression:{{ INDEXOF(about.4, '@') }}
User's response:email@email.com
Index returned:5

Extending on the previous example, a group of characters can be used within the expression. In these cases, the index of the first character within the group of characters stated within the expression will be the index given in the response.

Identifier:about
Question reference:about.4
Expression:{{ INDEXOF(about.4, '.com') }}
User's response:email@email.com
Index returned:11

If identifying information is collected using a multi-column question as names, email addresses, phone numbers, etc., we would need to adjust the formatting of the expression in order to target a single cell within the grid. 

Row with one cell selected
Identifier:about
Question reference:about[1]
Expression:{{ INDEXOF(about[1], '@') }}
User response:email@email.com
Index returned:5