🚀 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 62.169.16.147 on 2026-09-12.

Note[edit source]

A string is logically an ordered collection of characters - but in MDriven and most other development languages Strings hold a unique position as a specific datatype of its own.

This MAY cause confusion as String has specific operators and DOES NOT work as a collection of chars.

Noteworthy is:

somestring->length is different than collection->size
somestring->contains('a') is different than collection->exists(x|x=vSomeObjectRef) 
somestring->ToCharArray actually turns the string into an array of chars

Definition[edit source]

A String is defined as a finite sequence of characters from some character set. It can include letters, digits, punctuation marks, and other symbols. The String data type represents sequences of characters.

Syntax[edit source]

The syntax for declaring a String variable in OCL is straightforward. For example:

  variableName: String
  This declares a variable named `variableName` of type String.

Operations[edit source]

OCL provides several operations that can be performed on String data types. Some common operations include:

  - Concatenation: Combining two strings together to form a new string.
  - Length: Determining the number of characters in a string.
  - Substring: Extracting a portion of a string.
  - Comparison: Comparing two strings for equality or ordering.
  - Conversion: Converting a string to upper case, lower case, or other formats.

Constraints[edit source]

In OCL, constraints can be applied to String data types to specify rules or conditions that must be satisfied. For example, constraints can specify the minimum or maximum length of a string, character patterns it must match, or any other relevant conditions.

Examples[edit source]

  - Concatenation:
    let str1: String = "Hello";
    let str2: String = " World";
    let result: String = str1.concat(str2);  -- Result will be "Hello World"
  - Length:
    let str: String = "Hello";
    let length: Integer = str.size();  -- Length will be 5
  - Substring:
    let str: String = "Hello World";
    let sub: String = str.substring(6, 5);  -- Substring will be "World"
  - Comparison:
    let str1: String = "apple";
    let str2: String = "banana";
    let isEqual: Boolean = str1 = str2;  -- isEqual will be false
  - Constraint:
    context Person
    inv: name.size() >= 2  -- Ensures that the name of a person must be at least 2 characters long

The String data type in OCL represents sequences of characters and provides operations and constraints for manipulating and constraining strings in model constraints and expressions.

Special string constants / String escape sequences[edit source]

To insert a row break use '\r\n'

To insert a tab use '\t'

See this link: What are all the escape characters? - Stack Overflow

See also: Number conversions


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