Skip to main content

How to display parts of a string

These Calculated Field expressions return part of a text value: its first or last few characters, or a range in between. Replace fieldAliasGoesHere with the alias of your Field.

Positions in a string count from 0, and the end position you give substring() is not included. So substring(0, 10) returns the characters at positions 0 through 9: the first ten.

Display the first 10 characters of a string​

Uses substring().

// Input: A string with value "Lorem ipsum dolor sit amet"
// Output: A new string with value "Lorem ipsu"

var exampleString = $record['fieldAliasGoesHere'];
var firstTenCharacters = exampleString.substring(0, 10);

firstTenCharacters;

Display the last 10 characters of a string with substr()​

Uses substr() and length.

// Input: A string with value "Lorem ipsum dolor sit amet"
// Output: A new string with value "r sit amet"

var exampleString = $record['fieldAliasGoesHere'];
var lastTenCharacters = exampleString.substr(exampleString.length - 10);

lastTenCharacters;

Display the last 10 characters of a string with slice()​

Uses slice(). A negative position counts back from the end of the string, so there is no need to work out the length first.

// Input: A string with value "Lorem ipsum dolor sit amet"
// Output: A new string with value "r sit amet"

var exampleString = $record['fieldAliasGoesHere'];
var lastTenCharacters = exampleString.slice(-10);

lastTenCharacters;

Display characters 2 through 8 of a string​

Uses substring(). The result starts at position 2 and stops before position 8.

// Input: A string with value "Lorem ipsum dolor sit amet"
// Output: A new string with value "rem ip"

var exampleString = $record['fieldAliasGoesHere'];
var substring = exampleString.substring(2, 8);

substring;

Display the characters of a string from position 5 to the end​

Uses substring(). Given only a start, substring() runs to the end of the string. The fifth character sits at position 4, because positions count from 0.

// Input: A string with value "Lorem ipsum dolor sit amet"
// Output: A new string with value "m ipsum dolor sit amet"

var exampleString = $record['fieldAliasGoesHere'];
var substring = exampleString.substring(4);

substring;