🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators ToCharArray
This page was created by Hans.karlsen on 2024-10-16. Last edited by Wikiadmin on 2026-07-29.

You can use toCharArray when an OCL expression needs the individual characters of a String, such as the character delimiter required by split.

Syntax

<stringExpression>.toCharArray

What it returns

toCharArray converts a String into an ordered collection (array) of characters.

For example:

'ABC'.toCharArray

The result contains the characters A, B, and C in that order.

Why convert a String?

A String is logically a sequence of characters, but it is a distinct datatype in MDriven OCL. String operators and collection operators are therefore not interchangeable. toCharArray is the conversion to use when an operator expects a collection of characters rather than a String.

For example, split expects its delimiter argument to be a character collection. A one-character literal such as ',' is still a String in OCL, so pass its character array instead:

'Red,Green,Blue'.split(','.toCharArray)

This produces the parts Red, Green, and Blue.

Common error with split

The following expression passes a String where split expects a collection of characters:

SomeString.split('X')

It can produce this error:

31:System.String does not conform to Collection(System.Char)

Convert the delimiter with toCharArray:

SomeString.split('X'.toCharArray)

Example: use characters as a collection

After conversion, you can use collection operations on the characters. This example selects random characters from a fixed character set when constructing a string:

let characters = 'ABCDEFGHJKLMNPQRST23456789'.toCharArray in (
  let wordlength = Int32.Random(5)+7 in (
    Sequence{1..wordlength}->collect(a|
      characters->at(Int32.Random(100).Mod(characters->size)+1)
    )->asSeparatedList('')
  )
)

Here, characters is a character collection. The expression uses its collection size and retrieves a character by position. See random for the complete random-number behavior.

Related String operations

Use String-specific operations when you do not need a character collection:

Need Use
Find the number of characters in a String length
Split text into parts using a character delimiter split with 'delimiter'.toCharArray
Convert text to uppercase or lowercase ToUpper or ToLower

See also