String Functions

String functions in QLingo perform tasks with text or string values. Most also return a result that is a string data type. Even if the input expression is a date or number data type, string functions will interpret the value as a string.

String functions in QLingo include:

Note: Click here to download a sample file containing a data set, plan and InDesign template using the example values and functions on this page.

The following table shows values used in the numeric function examples below. You can also download the full sample file for additional context.

LCase function

The LCase function converts all uppercase letters in the input value to lowercase. Only uppercase characters are changed. Lowercase letters and non-letter characters remain unchanged. This function returns a string data type.

Syntax

LCase(exprAsString)

The parameter for this function needs to be a string literal constant or a string formula (expression).

Examples

  1. Convert a static text string to lowercase.

    LCase("Hello WORLD")
    Result: "hello world"

  2. Lowercase the recipient's [firstname] value.

    LCase(|->[firstname])

    Recipient #1

    Field: [firstname] = "Jane "
    Result: "jane "

  3. Concatenate the recipient's [firstname] and [lastname] fields with a space in between, then return the result in lowercase format.

    LCase(|->[firstname] & " " & |->[lastname])

    Recipient #1

    Field: [firstname] = "Jane "; [lastname] = "Jones"
    Result: "jane jones"

  4. Remove any spaces from the recipient's [firstname] and [lastname] fields, concatenate the two fields with a space in between, then return the result in lowercase format.

    LCase(Trim(|->[firstname]) & " " & Trim(|->[lastname]))

    Recipient #1

    Fields:
    [firstname] = "Jane "
    [lastname] = "Jones"

    Calculation:
    Trim("Jane ") = "Jane"
    Trim("Jones") = "Jones"
    "Jane" & " " & "Jones" = "Jane Jones"
    LCase("Jane Jones") = "jane jones"

    Result: "jane jones"

UCase function

The UCase function converts all lowercase letters in the input value to uppercase. Only lowercase characters are changed. Uppercase letters and non-letter characters remain unchanged. This function returns a string data type.

Syntax

UCase(exprAsString)

The parameter for this function needs to be a string literal constant or a string formula (expression).

Examples

  1. Convert a static text string to uppercase.

    UCase("Hello WORLD")
    Result: "HELLO WORLD"

  2. Uppercase the recipient's [firstname] value.

    UCase(|->[firstname])

    Recipient #1

    Field: [firstname] = "Jane "
    Result: "JANE "

  3. Remove any spaces from the recipient's [firstname] and [lastname] fields, concatenate the two fields with a space in between, then return the result in uppercase format.

    UCase(Trim(|->[firstname]) & " " & Trim(|->[lastname]))

    Recipient #1

    Fields:
    [firstname] = "Jane "
    [lastname] = "Jones"

    Calculation:

    Trim("Jane ") = "Jane"
    Trim("Jones") = "Jones"
    "Jane" & " " & "Jones" = "Jane Jones"
    UCase("Jane Jones") = "JANE JONES"

    Result: "JANE JONES"

  4. Remove any space characters from the [state] field, and return the result in uppercase format.

    UCase(Trim(|->[state]))

    Recipient #1

    Field: [state] = "MT "
    Result: "MT"

    Recipient #2

    Field: [state] = "mt"
    Result: "MT"

TCase function

The TCase function converts the first letter of each word in the input string to uppercase, and all remaining letters in each word to lowercase. Numbers and non-letter characters remain unchanged. This function returns a string data type.

Syntax

TCase(exprAsString)
The parameter for this function needs to be a string literal constant or a string formula (expression).

Examples

  1. Convert a static text string to title case.

    TCase("Hello WORLD")
    Result: "Hello World"

  2. Title case the recipient's [city] database value.

    TCase(|->[city])

    Recipient #1

    Field: [city] = "missoula"
    Result: "Missoula"

    Recipient #2

    Field: [city] = "MISSOULA"
    Result: "Missoula"

  3. Concatenate the recipient's [firstname] and [lastname] fields with a space in between, then return the result in titlecase format.

    TCase(|->[firstname] & " " & |->[lastname])

    Recipient #1

    Fields:
    [firstname] = "Jane "
    [lastname] = "Jones"

    Result: "Jane Jones"

Length function

The Length function returns the number of characters in a string. While typically used with text, it can also accept a number as input. In such cases, the number is treated as a string, and all characters - including the decimal point, if present- are counted. This function returns a number data type.

Syntax

Length(exprAsString)
The parameter for this function will be treated as string regardless of the data type.

Examples

  1. Count the characters in a static string.

    Length("Hello WORLD")
    Result: 11

  2. Count the characters in a static decimal number.

    Length(3.14159)
    Result: 7

  3. Count the characters in the recipients [firstname] field.

    Length(|->[firstname])

    Recipient #2

    Field: [firstname] = "Jerry"
    Result: 5

    Recipient #4

    Field: [firstname] = "Sam"
    Result: 3

  4. Use the Length function in QLingo to evaluate the total character count of [firstname] and [lastname]. If the combined length is less than 11 characters, return both names. If it exceeds the limit, return only the first name.

    Scenario: Your document’s cover has limited space, and you want to display the recipient’s full name only if it fits. If the combined length of the first name and last name is 10 characters or fewer, show both. Otherwise, display only the first name.

    Copy
    if ( Length( Trim(|->[firstname]) & Trim(|->[lastname]) ) < 11 )
    {
      Trim(|->[firstname]) & " " & Trim(|->[lastname])
    }
    Else
    {
      Trim(|->[firstname])
    }

    Recipient #1

    Fields:
    [firstname] = "Jane "
    [lastname] = "Jones"

    Calculation:

    Trim("Jane ") = "Jane"
    Trim("Jones") = "Jones"
    Length("JaneJones") = 9
    9 < 11 = true

    Result: "Jane Jones"

    Recipient #3

    Fields:
    [firstname] = " Sandra "
    [lastname] = "Smith"

    Calculation:

    Trim(" Sandra ") = "Sandra"
    Trim("Smith") = "Smith"
    Length("SandraSmith") = 11
    11 < 11 = false

    Result: "Sandra"

IsNullOrEmpty function

The IsNullOrEmpty function determines if the input parameter is NULL (for example, a NULL data source value) or an empty string. This function returns a boolean data type result: true if the string is NULL or empty, and false otherwise.

Syntax

IsNullOrEmpty(exprAsString)
The parameter for this function will be treated as string regardless of the data type.

Examples

  1. Test IsNullOrEmpty on static strings.

    IsNullOrEmpty("")
    Result: true

    IsNullOrEmpty("Hello WORLD")
    Result: false

  2. Test to see if the data source includes the recipient's [mobile] number.

    IsNullOrEmpty(|->[mobile])

    Recipient #1

    Field: [mobile] = " (417) 123-1456"
    Result: false

    Recipient #8

    Field: [mobile] = ""
    Result: true

  3. Use the IsNullOrEmpty function in QLingo to check whether the [mobile] field contains a value. If it does, return the mobile number prefixed with "Mobile: ". If the field is empty or null, return an empty string so that nothing appears.

    Scenario: You are creating business cards and want to display a prefixed label before the mobile number, only if there is a mobile number in the data source. If there is no [mobile], just return an empty string so that nothing appears on the business card.

    Copy
    if (IsNullOrEmpty(|->[mobile]))
    {
       ""
    }
    else
    {
       "Mobile: " & |->[mobile]
    }

    Recipient #1

    Field: [mobile] = " (417) 123-1456"
    Result: "Mobile: (417) 123-1456"

    Recipient #8

    Field: [mobile] = ""
    Result: ""

SubString function

The SubString function returns part of a string based on the values of two other parameters: The index of the first character to return; and the number of characters to return. This function returns a string data type.

Syntax

SubString(exprAsString, exprAsNumber, exprAsNumber)

The first parameter is the value you want to retrieve text from. It will be treated as string regardless of the input data type.

The second parameter is the starting position in the string (zero based) where the first character is position 0. This parameter must be a number data type.

The third parameter is how many characters to return. This parameter must be a number data type. If you want to return all characters after the start position, you can set this parameter to -1.

Examples

  1. Return the first five characters in a static string.

    SubString("Hello WORLD", 0, 5)
    Result: "Hello"

  2. Return the first letter of the [maritalStatus] data field.

    SubString(|->[maritalStatus], 0, 1)

    Recipient #1

    Field: [maritalStatus] = "married"
    Result: "m"

    Recipient #5

    Field: [maritalStatus] = "single"
    Result: "s"

  3. Return the item code from the [SKU] recipient data field.

    Scenario: The item last purchased by the recipient is recorded in the [SKU] data field. The SKU is defined by Gender (W/M), Category code (T/J), Color code (BL/BK/RD) and item number at the end. To get the item code, select all characters after the 4th character.

    SubString(|->[SKU], 4, -1)

    Recipient #1

    Field: [SKU] = "WTRD102"
    Result: "102"

    Recipient #2

    Field: [SKU] = "MTRD102"
    Result: "102"

  4. Return only the first sentence from the [comment] data source field.

    SubString(|->[comment], 0, Find(|->[comment], ".", 0) + 1)

    Recipient #1

    Field: [comment] = "Etiam justo. Etiam pretium iaculis justo. In hac habitasse platea dictumst. Etiam faucibus cursus urna. Ut tellus."

    Calculation:

    The Find() function locates the first "." at position 11
    The SubString() function returns the text from start (character 0) and returns 12 characters (11 + 1). One (1) is added to the result of the Find() function because, being zero-based, the first character is at position 0 and not 1.

    Result: "Etiam justo."

    Recipient #2

    Field: [comment] = "Nullam molestie nibh in lectus. Pellentesque at nulla. Suspendisse potenti. Cras in purus eu magna vulputate luctus. Vestibulum rutrum rutrum neque."

    Calculation:

    The Find() function locates the first "." at position 30
    The SubString() function returns the text from start (character 0) and returns 31 characters (30 + 1).

    Result: "Nullam molestie nibh in lectus."

Trim function

The Trim function removes any white space characters from both the start and end of the input string parameter. White space characters include the space, tab, newline (forced line break) and return (end of paragraph) characters. This function returns a string data type.

Syntax

Trim(exprAsString)
The first parameter exprAsString will be treated as string regardless of the data type.

Examples

  1. Trim white spaces from a static string.

    Trim(" Hello WORLD ")
    Result: "Hello WORLD"

  2. Trim white spaces from the customers [firstname] data source value.

    Trim(|->[firstname])

    Recipient #1

    Field: [firstname] = "Jane "
    Result: "Jane"

    Recipient #3

    Field: [firstname] = " Sandra "
    Result: "Sandra"

LTrim function

The LTrim function removes any white space characters from the leading or left-side of the input string parameter. White space characters include the space, tab, newline (forced line break) and return (end of paragraph) characters. This function returns a string data type.

Syntax

LTrim(exprAsString)
The first parameter exprAsString will be treated as string regardless of the data type 

Examples

  1. Trim leading white spaces from a static string.

    LTrim(" Hello WORLD ")
    Result: "Hello WORLD "

  2. Trim leading white spaces from the customers [firstname] data source value.

    LTrim(|->[firstname])

    Recipient #1

    Field: [firstname] = "Jane "
    Result: "Jane "

    Recipient #3

    Field: [firstname] = " Sandra "
    Result: "Sandra "

RTrim function

The RTrim function removes any white space characters from the trailing or right-side of the input string parameter. White space characters include the space, tab, newline (forced line break) and return (end of paragraph) characters. This function returns a string data type.

Syntax

RTrim(exprAsString)
The first parameter exprAsString will be treated as string regardless of the data type.

Examples

  1. Trim trailing white spaces from a static string.

    RTrim(" Hello WORLD ")
    Result: " Hello WORLD"

  2. Trim trailing white spaces from the customers [firstname] data source value.

    RTrim(|->[firstname])

    Recipient #1

    Field: [firstname] = "Jane "
    Result: "Jane"

    Recipient #3

    Field: [firstname] = " Sandra "
    Result: " Sandra"

Find function

The Find function returns the zero-based index of a substring found within a larger string. If the search text is not found, the function returns -1. The Find function is case-sensitive and returns a number data type.

Syntax

Find(exprAsString, exprAsString, exprAsNumber)

The first parameter is the text you want to search through. It will be treated as string regardless of the actual data type.

The second parameter is the is the specific string you are trying to find inside the first parameter.

The third parameter must be a number, and it tells the function where to start looking. It uses zero-based indexing, so 0 means to search from the first character.

Examples

  1. Find text in a static string.

    Find("Hello WORLD", "Hello", 0)
    Result: 0

    Find("Hello WORLD", "WORLD", 0)
    Result: 6

    Find("Hello WORLD", "world", 0)
    Result: -1

    Find(LCase("Hello WORLD"), "world", 0)
    Result: 6

  2. Identify if the product in the [SKU] is a t-shirt

    Scenario: The item last purchased by the recipient is recorded in the [SKU] data field. The SKU is defined by Gender (W/M), Category code (T/J), Color code (BL/BK/RD) and item number at the end. The category code T = t-shirt, J = jeans.

    Copy
    if(Find(|->[SKU], "T", 0) > 0)
    {
        "T-shirt"
    }
    else
    {
        "not a T-shirt"
    }

    Recipient #1

    Field: [SKU] = "WTRD102"
    Result: "T-shirt"

    Recipient #3

    Field: [SKU] = "WJBL253"
    Result: "not a T-shirt"

Replace function

The Replace function replaces a specific range of characters in a string with another string. This function returns a string data type.

Syntax

Replace(exprAsString, exprAsString, exprAsNumber, exprAsNumber)

The first parameter is the text you want to edit. It will be treated as string regardless of the actual data type.

The second parameter is the specific string you want to insert into the first parameter.

The third parameter is the zero-based index where you want to insert the second parameter.

The fourth parameter is the number of characters in the first parameter that you want the insert to replace. If inserting rather than replacing, then this value can be 0.

Examples

  1. Replace text in a static string.

    Replace("Hello WORLD", "UNIVERSE", 6, 5)
    Result: "Hello UNIVERSE"

  2. Insert text into a static string.

    Replace("Hello WORLD", "brave new ", 6, 0)
    Result: "Hello brave new WORLD"

  3. Replace "St." with "Street" in the [address1] field

    Scenario: In your address block, you prefer to spell out "Street" in full if the [address1] field includes the text "St.".

    Copy
    if(Find(|->[address1], "St.") > -1)
    {
        Replace(|->[address1], "Street", Find(|->[address1], "St."), 3)
    }
    else
    {
        |->[address1]
    }

    Recipient #1

    Field: [address1] = " 756-9580 Lectus St."
    Result: " 756-9580 Lectus Street"

    Recipient #5

    Field: [address1]: "6094 Fames Road"
    Result: "6094 Fames Road"

FindAndReplace function

The FindAndReplace function searches for a specific substring within a larger string and replaces it with a new value. This function is case-sensitive and returns a string data type.

Syntax

FindAndReplace(exprAsString, exprAsString, exprAsString)

The first parameter is the text you want to search through. It will be treated as string regardless of the actual data type.

The second parameter is the specific string you are trying to find inside the first parameter.

The third parameter is the string that will replace the matched string.

Examples

  1. Replace text in a static string.

    FindAndReplace("Hello WORLD", "WORLD", "UNIVERSE")
    Result: "Hello UNIVERSE"

    FindAndReplace("Hello WORLD", "World", "Universe")
    Result: "Hello WORLD"

    Copy
    TCase(
      FindAndReplace(
        LCase("Hello WORLD"),
        "world",
        "Universe"
      )
    )

    Result: "Hello Universe"

  2. Replace "St." with "Street" in the [address1] field

    Scenario: In your address block, you prefer to spell out "Street" in full if the [address1] field includes the text "St.".

    FindAndReplace(|->[address1], "St.", "Street")

    Recipient #1

    Field: [address1] = " 756-9580 Lectus St."
    Result: " 756-9580 Lectus Street"

    Recipient #5

    [address1]: "6094 Fames Road"
    Result: "6094 Fames Road"

FindAndReplaceChars function

The FindAndReplaceChars function replaces all instances of one or more characters in a string with another set of characters. This function returns a string data type.

Syntax

FindAndReplaceChars(exprAsString, exprAsString, exprAsString)

The first parameter is the text you want to search through. It will be treated as string regardless of the actual data type.

The second parameter is a list of characters that you want to replace. Each character is treated independently.

The third parameter is the string that will replace the matched character.

Examples

  1. Replace characters that are not permitted in a Windows filename with the underscore character.

    Copy
    FindAndReplaceChars(
      "<Is>: this\"| \\a/ good*filename?", 
      "<>:\"/\\|?*", 
      "_"
    )

    Result: " _Is__ this__ _a_ good_filename_"

  2. Remove "()-" and space characters from the [phone] data source field.

    FindAndReplaceChars(|->[phone], "()- ", "")

    Recipient #1

    Field: [phone] = " 261 784-3674"
    Result: " 2617843674"

    Recipient #4

    Field: [phone] = " (882) 160-9155"
    Result: "8821609155"

FindByRegExp function

The FindByRegExp function is designed to return only the text that is matched by a regular expression. This function returns a string data type.

Default regular expression grammar options:

These default grammar options can be changed. For further details, contact XMPie Support.

Syntax

FindByRegExp(exprAsString, regExpString, firstMatchOnly)

The first parameter is the text you want to search through. It will be treated as string regardless of the actual data type.

The second parameter is a regular expression.

The third parameter is a boolean. If set to true, only the first match is returned. If set to false, all matching results are returned.

Examples

  1. Select the first word of a static string by matching one or more upper- or lower-case letters only.

    FindByRegExp("Hello WORLD", "[A-Za-z]+", true)
    Result: "Hello"

  2. Find the file type extension from the [filename] in the recipient data source.

    FindByRegExp(|->[filename], "\\.[a-zA-Z0-9]{1,5}$", true)

    Recipient #1

    Field: [filename] = "Cras.mp3"
    Result: ".mp3"

    Recipient #2

    Field: [filename] = "VellpsumPraesent.docx"
    Result: ".docx"

  3. Return only the digits from the [phone] in the recipient data source.

    FindByRegExp(|->[phone], "\\d+", false)

    Recipient #1

    Field: [phone] = "261 784-3674"
    Result: "2617843674"

    Recipient #2

    Field: [phone] = "939 171 7393"
    Result: "9391717393"

FindAndReplaceByRegExp function

The FindAndReplaceByRegExp function finds the text that is matched by the regular expression, and replaces it with a specific string value. This function returns a string data type..

Default grammar options:

These default grammar options can be changed. For further details, contact XMPieSupport.

Syntax

FindAndReplaceByRegExp(exprAsString, regExpString, exprAsString, firstMatchOnly)

The first parameter is the text you want to search through. It will be treated as string regardless of the actual data type.

The second parameter is a regular expression.

The third parameter is the string that will replace the matched text.

The fourth parameter is a boolean. If set to true, only the first match is replaced. If set to false, all matching results are replaced.

Examples

  1. Replace the first word of a static string by matching one or more upper- or lower-case letters only.

    FindAndReplaceByRegExp("Hello WORLD", "[A-Za-z]+", "Hi", true)
    Result: "Hi WORLD"

  2. Remove the file extension from the [filename] by replacing it with an empty string.

    FindAndReplaceByRegExp(|->[filename], "\\.[^.]*$", "", False)

    Recipient #1

    Field: [filename] = "Cras.mp3"
    Result: "Cras"

    Recipient #2

    Field: [filename] = "VellpsumPraesent.docx"
    Result: "VellpsumPraesent"

  3. Remove any non-digits from the [phone] in the recipient data source by replacing them with an empty string.

    FindAndReplaceByRegExp(|->[phone], "[^\\d]+", "", false)

    Recipient #1

    Field: [phone] = "261 784-3674"
    Result: "2617843674"

    Recipient #2

    Field: [phone] = "939 171 7393"
    Result: "9391717393"

CleanNumber function

The CleanNumber function removes all non-digit characters from a string and returns only the digits. This function returns a string data type.

Although the result contains numeric characters, it is returned as a string data type to prevent errors caused by long digit sequences. QLingo supports numeric values up to 14 digits, so longer sequences must be handled as strings to ensure accuracy.

Syntax

CleanNumber(exprAsString)

Examples

  1. Remove any non-digit characters from a static string.

    CleanNumber("+1 888-452-1111")
    Result: "18884521111"

  2. Remove any non-digit characters from the [phone] recipient data source value.

    CleanNumber(|->[phone])

    Recipient #1

    Field: [phone] = "261 784-3674"
    Result: "2617843674"

    Recipient #2

    Field: [phone] = "939 171 7393"
    Result: "9391717393"

CleanRecipientKey function

The CleanRecipientKey function removes or replaces any characters from a string that are not recommended to use in a URL. Specifically, the function will remove or replace these characters: space " ", single quotes ', double quotes ", colon :, question mark ?, ampersand &, asterisk *, pound/hash #, less-than <, greater-than >, and pipe |.

This is particularly useful for XMPie's Cross Media products, where a data source field is used in the URL as the primary key to identify the recipient. This function returns a string data type.

Syntax

CleanRecipientKey(exprAsString, ReplaceInvalidWith)

The first parameter is the text you want to clean. It will be treated as string regardless of the actual data type.

The second parameter is optional. If omitted or empty, any unsafe URL characters will be deleted from the first parameter. If a string is provided as the second parameter, any unsafe URL characters will be replaced by this string.

Examples

  1. Remove any unsafe URL characters from a static string.

    CleanRecipientKey("John Michael.Smith")
    Result: "JohnMichael.Smith"

  2. Replace any unsafe URL characters in a static string with the underscore character.

    CleanRecipientKey("John Michael.Smith", "_")
    Result: "John_Michael.Smith"

  3. Concatenate the [firstname] and [lastname] fields with a dot in between and remove any unsafe URL characters from the result.

    CleanRecipientKey(|->[firstname] & "." & |->[lastname])

    Recipient #1

    Field: [firstname] = "Jane "; [lastname] = "Jones"
    Result: "Jane.Jones"

    Recipient #2

    Field: [firstname] = "Jerry"; [lastname] = "Jones"
    Result: "Jerry.Jones"

SecureID function

The SecureID function generates a unique, non-guessable and secure recipient ID. For example: "5ADCF67B419A4AD796DA4458D25A038E".

This is particularly useful for XMPie's Cross Media campaigns, where you do not want to display personal identifiable information (PII) in the URL. This function returns a string data type.

Note: While this function is not available in the uCreate Print rule editor's dropdown list, you can click the Edit QLingo button and type in the name of the function to use it with uCreate Print.

Syntax

SecureID()

The function does not require any parameters.

Example

Create a secure ID or random string.

SecureID()
Result: "80BA0018F0A446DC8D15AC6794CFF5CF"

HexToUnicode function

The HexToUnicode function converts a string of hexadecimal values into their unicode character equivalents. This function returns a string data type.

Each sequence of four hexadecimal characters is converted into the matching unicode character. If the input string cannot be evenly divided into four, the function automatically pads the string with leading zeros.

Syntax

HexToUnicode(exprAsHexString)

The parameter for this function needs to be a string of hexadecimal characters.

Examples

  1. Concatenate two static string values with a tab (hex 0009).

    "Hello" & HexToUnicode(0009) & "WORLD"
    Result: "Hello<tab>WORLD"

  2. Add the Euro sign (hex 20AC) to a static string.

    HexToUnicode(20AC) & "123"
    Result: "€123"

HtmlEncode function

The HtmlEncode function encodes a string so that any characters that are unsafe or reserved in a URL are converted to a format that can be used without confusion or error. Sometimes called "percent encoding" because the encoded characters are shown as a % followed by two hexadecimal digits representing the characters' ASCII value.

This function is particularly useful for XMPie's Cross Media products, where a data source value may need to be used in a URL. This function returns a string data type.

Syntax

HtmlEncode(exprAsString)

The parameter for this function needs to be a string constant, or string formula (expression).

Examples

  1. Encode a static string for use in a URL.

    "https://www.server.com/docs/" & HtmlEncode("John&Mary.pdf")
    Result: "https://www.server.com/docs/John%26Mary.pdf"

  2. Encode a dynamic string for use in a URL.

    Copy
    "https://www.server.com/docs/" & 
    HtmlEncode(
       |->[firstname] & 
       "&" 
       & |->[lastname] & 
       ".pdf"
    )

    Recipient #1

    Fields:
    [firstname] = "Jane "
    [lastname] = "Jones"

    Result: "https://www.server.com/docs/Jane%20%26Jones.pdf"

    Recipient #2

    Fields:
    [firstname] = "Jerry"
    [lastname] = "Jones"

    Result: "https://www.server.com/docs/Jerry%26Jones.pdf"