Start page / Template development / Template syntax / Expressions / String operations

String operations

Evaluation of string operations

A range of methods can be invoked on the String data type , for example to join two separate character strings or to extract a substring from a character string. The expressions required for this are explained in the following:

  • Expressions for joining character strings (string concatenation)
  • Expressions for extracting substrings

Expressions for joining character strings (string concatenation)

Concatenation is an operation which can be applied to character strings (or to list-type data structures). A character string consists of a sequence of characters in a defined order. String concatenation can be used to merge two character strings into one character string without changing the order of the individual characters. The first part of the new merged character string is formed from the first character string and the second part is formed from the second character string.

Example: The two character strings "First" and "Spirit" can be merged using the following expression to form the character string "FirstSpirit":

"First" + "Spirit"

or analogously via variables:

str1 + str2

To this extent:

$CMS_SET(str1, "First")$
$CMS_SET(str2, "Spirit")$

In this way it is possible to link an existing variable of String data type with a new character string:

$CMS_SET(myStr, str1 + str2)$
$CMS_VALUE(myStr)$
$CMS_SET(myStr, myStr + "_V5.1")$
$CMS_VALUE(myStr)$

Output:

FirstSpirit
FirstSpirit_V5.1

Expressions for extracting substrings

There are different expressions for extracting a substring from a character string. The characters of a character string have a defined order and can be determined via an index. This means, by specifying:

  • character string
  • Start index
  • End index

, a substring can be uniquely defined.

The simplest way is to extract a character from a character string by specifying an individual index value, which is simultaneously evaluated as the start and end index (start index = end index). This means the expression:

"CharacterString"[index]

returns as a substring precisely the character of the "CharacterString", which is located in the indexed position of the character string. I.e.:

"Text"[0]

returns the substring:

"T"

To extract a substring which consists of more than one character, two index values must be given, separated by '..' whereby: End index > Start index:

"CharacterString"[startIndex..endIndex]

Example: The character string 'myStr' with the value "FirstSpirit" from the first example is to be split back into two substrings:

myStr[0..4]

The code example extracts the substring from the first element (0) up to (..) to the fifth element (4) of the character string. The evaluation produces the following result:

"FIRST"

The next part of the character string is to be extracted from the sixth element (5) up to (..) to the last element of the character string:

myStr[5..10]

The evaluation produces the following result:

"spirit"

If the length of the character string is unknown, the following expression can be used analogously:

 myStr[5..endIndex]

If:

$CMS_SET(endIndex, myStr.length - 1)

'myStr.length' determines the length of the character string 'myStr' and thus returns the last 'Index value + 1' of the character string.

© 2005 - 2024 Crownpeak Technology GmbH | All rights reserved. | FirstSpirit 2024.13 | Data privacy