🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
String
This page was created by Stephanie on 2023-05-19. Last edited by Wikiadmin on 2026-07-29.

A String stores text as a finite sequence of characters; use it in OCL expressions, attributes, and method parameters when you need values such as names, messages, or codes.

String is a value type

In MDriven, a String is a specific data type, not a collection of characters. Although text is logically made of characters, do not treat a String as a collection in OCL.

For example, use the String operation length to find the number of characters in a value:

'Hello'.length

This returns 5. Do not use collection operations such as size for a String.

A String is also not an object in OCL. This matters when an operator requires an object parameter: a String is not a valid substitute for an object. For the distinction between objects and values, see Documentation:Objects.

Declare and use String values

Use String as the type name in a declaration or method signature.

variableName: String

For example, a query method that accepts and returns text can have this signature:

FormatName(name:String):String

For method parameters, return types, and query methods, see Documentation:Methods.

String literals

Write a text literal in single quotes.

'Hello World'

You can include escape sequences in a literal when you need formatting characters.

Escape sequence Inserts Example use
\r\n A row break 'First row\r\nSecond row'
\t A tab 'Name\tValue'

Common String operations

Join text with concat

Use concat to append one String to another. It returns a new String containing the value before .concat followed by its argument.

'Hello'.concat(' World')

The result is Hello World. See concat for the operation definition.

Get the character count with length

Use length when a rule depends on the amount of text.

self.Name.length

For example, the expression below is true when Name contains at least two characters:

self.Name.length >= 2

Compare String values

Use = to test whether two String values are equal.

self.Status = 'Approved'

This expression is true only when the value of Status is Approved.

Check text content

contains is a String operation. For example, the following checks whether a value contains the character a:

self.Name.contains('a')

Do not replace this with a collection expression such as exists; collection operations and String operations have different purposes.

Extract part of a String

Use subString to take a part of a String. A common use is to shorten a value before storing it in an attribute with a fixed maximum length.

self.errorinfo.subString(1, self.errorinfo.maxLength-1)

This pattern prevents an attempt to save text that is too long for the attribute. Read String attribute overflowing for the full problem and solution, and maxLength for the attribute-length operation.

Avoid attribute overflow

A String attribute can have a maximum allowed length. If OCL or another part of the system assigns more text than the attribute can store, saving can fail with a database truncation error.

When you intentionally keep only the beginning of a value, use the attribute's maxLength rather than a hard-coded number. For example:

self.errorinfo.subString(1, self.errorinfo.maxLength-1)

Also make the attribute long enough for the information you need to retain. See Documentation:String attribute overflowing before applying truncation, because truncating text discards the remainder.

Convert encoded text to a BLOB

When a String contains Base64-encoded data, you can convert it to a byte array (BLOB) with Base64ToBlob.

'My clear text string'.StringToBase64.Base64ToBlob

See Base64ToBlob for this conversion.

See also

[[Category:Attributes and data types [Beginner]]]