1. Home
  2. Docs
  3. Personalization
  4. Reference
  5. String Manipulation Functions

String Manipulation Functions

These string functions build strings based on other strings. They can be used either functionally (e.g. left(“Philip”,3)) or in pipe mode (e.g “Philip” left 3).

In all of the examples, string literals are used. However these will typically be custom fields in a real templating implementation e.g. <$if left(Name,4) == “phil”$>Hi, Phil<$endif$>. In addition, in a template, these functions would need to be inside an <$if$> or <$eval$> tag. These tags have been omitted from this page for clarity.

left

Returns the leftmost N characters of a string S. If the string is shorter than N, the whole string is returned.

Usage

  • Left(S,N)
  • S Left N

Examples

  • “philip” Left 4 – returns “phil”
  • left(“phil”,10) – returns “phil”
  • “philip” left 0 – returns “” (an empty string)

right

Returns the rightmost N characters of a string S. If the string is shorter than N, the whole string is returned.

Usage

  • Right(S,N)
  • S Right N

Examples

  • “philip” Right 4 – returns “ilip”
  • right(“phil”,10) – returns “phil”
  • “philip” right 0 – returns “” (an empty string)

mid

Returns the string starting at position N. If S is shorted than N, an empty string is returned. Note: the first character is at position 0 (zero).

Usage

  • Mid(S,N)
  • S Mid N

Examples

  • “philip” Mid 1 – returns “hilip”
  • “philip” Mid 4 – returns “ip”
  • mid(“phil”,10) – returns “” (an empty string)

find

Returns the location of the second string X in the first. If the string is not found, it returns -1.

Usage

  • Find(S,X)
  • S Find X

Examples

  • “philip” find “phil” – returns 0
  • Find(“phil”,”lip”) – returns -1
  • “philip” find “lip” – returns 3

replace

Replaces every occurrence of string X in S with Y.

Usage

  • Replace(S,X,Y)
  • S Replace X,Y

Examples

  • “One fish, two fish, red fish, blue fish” replace “fish”,”sheep”- returns “One sheep, two sheep, red sheep, blue sheep”
  • replace(“One fish, two fish, red fish, blue fish”,”one”,”No”) – returns “No fish, two fish, red fish, blue fish”

replacefirst

Replaces only the first occurrence of string X in S with Y.

Usage

  • Replacefirst(S,X,Y)
  • S Replacefirst X,Y

Examples

  • “One fish, two fish, red fish, blue fish” replacefirst “fish”,”sheep”- returns “One sheep, two fish, red fish, blue fish”
  • replacefirst(“One fish, two fish, red fish, blue fish”,”fish”,”ring”) – returns “One ring, two fish, red fish, blue fish”

replacelast

Replaces the last occurrence of string X in S with Y.

Usage

  • Replacelast(S,X,Y)
  • S Replacelast X,Y

Examples

  • “One fish, two fish, red fish, blue fish” replacelast “fish”,”sheep”- returns “One fish, two fish, red fish, blue sheep”
  • replacelast(“One fish, two fish, red fish, blue fish”,”fish”,”note”) – returns “One ring, two fish, red fish, blue note”

reverse

Reverses the string.

Usage

  • Reverse(S)
  • S Reverse

Examples

  • “Phil” reverse – returns “lihP”
  • reverse(“able was I”) – returns “I saw elba”

reversefind

Returns the last location of the second string X in the first. If the string is not found, it returns -1.

Usage

  • Reversefind(S,X)
  • S Reversefind X

Examples

  • “One fish, two fish” reversefind “fish” – returns 14
  • reversefind(“One fish, two fish”,”one”) – returns 0
  • reversefind(“One fish, two fish”,”Phil”) – returns -1

substr

Returns the Y characters of S starting at position X, if X is positive. If X is negative, X is the number of characters from the end of the string.

Usage

  • Substr(S,X,Y)
  • S substr X,Y

Examples

  • “janfebmaraprmayjun” substr -6,3 – returns “may”
  • substr(“janfebmaraprmayjun”,3,3) – returns “feb”

truncate

Truncates S at up to N characters, adding three periods (dots) as an ellipsis. The function works back from the Nth character to the closest whitespace character to avoid splitting inside a word. If there’s no available whitespace, though, the word will be split. In other words, truncating “the quick brown fox” to 13 characters gives “the quick…” instead of “the quick bro…”, but truncating it to 3 characters gives “hel…”. If the string is shorter than N, no truncation takes place, nor is an ellipsis added.

Usage

  • truncate(S,N)
  • S truncate N

Examples

  • “the quick brown fox jumps over the lazy dog” truncate 21 returns “”the quick brown fox…”
  • truncate(“hello, world”,5) returns “hello…”
  • truncate(“hello, world”,500) returns “hello, world”

truncatewords

Truncates S at up to N words, adding a space and then three periods (dots) as an ellipsis. If the string is shorter than N words, no truncation takes place, nor is an ellipsis added. If a word is followed by punctuation that punctuation is included in the word.

Usage

  • truncatewords(S,N)
  • S truncatewords N

Examples

  • “the quick brown fox jumps over the lazy dog” truncatewords 4 returns “the quick brown fox …”
  • truncatewords(“hello, world”,1) returns “hello, …”
  • truncatewords(“hello, world”,5) returns “hello, world”

remove

Removes all occurrences of X from S.

Usage

  • Remove(S,X)
  • S Remove X

Examples

  • “One fish, two fish, red fish, blue fish” remove ” fish” – returns “One, two, red, blue” (Note leading space)
  • remove(“One fish, two fish, red fish, blue fish”,”fish,”) – returns “One two red blue fish” (note comma)

removefirst

Removes the first occurrence of X from S.

Usage

  • Removefirst(S,X)
  • S Removefirst X

Examples

  • “One fish, two fish, red fish, blue fish” removefirst ” fish” – returns “One, two fish, red fish, blue fish” (Note leading space)
  • removefirst(“One fish, two fish, red fish, blue fish”,”fish,”) – returns “One two fish, red fish, blue fish” (note comma)

removelast

Removes the last occurrence of X from S.

Usage

  • Removelast(S,X)
  • S Removelast X

Examples

  • “One fish, two fish, red fish, blue fish” removelast ” fish” – returns “One fish, two fish, red fish, blue” (Note leading space)
  • removelast(“One fish, two fish, red fish, blue fish”,”fish,”) – returns “One fish, two fish, red blue fish” (note comma)

uppercase

Converts the string to UPPERCASE.

Usage

  • Uppercase(S)
  • S Uppercase

Examples

  • uppercase(“phil’) – returns “PHIL”
  • “stop” uppercase – returns “STOP”

lowercase

Converts the string to all lower case.

Usage

  • Lowercase(S)
  • S lowercase

Examples

  • lowercase(“BEGIN NOW’) – returns “begin now”
  • “Woah!” lowercase – returns “woah!”

titlecase

Capitalizes the first letter of the first word, and all words longer than three letters. All caps letters of three letters or less are untouched, shorter words are concerted to lowercase.

Usage

  • TItlecase(S)
  • S titlecase

Examples

  • “TBD: the quick brown fox, DNA is an acronym etc” titlecase – returns “TBD: the Quick Brown Fox, DNA is an Acronym etc”

propercase

Capitalizes the first letter of the first word, and all words longer than three letters. All caps letters of three letters or less are lowercased, shorter words are converted to lowercase.

Usage

  • propercase(S)
  • S propercase

Examples

  • “TBD: the quick brown fox, DNA is an acronym etc” propercase- returns “Tbd: the Quick Brown Fox, dna is an Acronym etc”

sentencecase

Capitalizes the first letter of the first word, lowercases all other words, unless the word is all caps and three letters long, in which case it is assumed to be an acronym.

Usage

  • sentencecase(S)
  • S sentence

Examples

  • “TBD: the quick brown fox, DNA is an acronym etc” sentencecase- returns “TBD: the quick brown fox, DNA is an acronym etc”

length

For a string S, the number of characters it contains.

Usage

  • length(S)
  • S length

Examples

  • length(“philip”) – returns 6
  • “Phil” length – returns 4

trim

Removes whitespace (space, tab, new line, carriage return) from the start and end of the string. Does not affect internal whitespace.

Usage

  • trim(S)
  • S trim

Examples

  • ” hello world ” trim – returns “hello world”
  • trim(” hello world “) – returns “hello world”

trimright

Removes whitespace (space, tab, new line, carriage return) from the right hand side of the string only. Does not affect leading or internal whitespace.

Usage

  • trimright(S)
  • S trimright

Examples

  • ” hello world ” trimright – returns ” hello world”
  • trimright(” hello world “) – returns ” hello world”

trimleft

Removes whitespace (space, tab, new line, carriage return) from the left hand side of the string only. Does not affect trailing or internal whitespace.

Usage

  • trimleft(S)
  • S trimleft

Examples

  • ” hello world ” trimleft – returns “hello world “
  • trimleft(” hello world “) – returns “hello world “