No edit summary |
No edit summary |
||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
=== <message>Write the content here to display this box</message>Note === | |||
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->[[Documentation:OCLOperators length|length]] is different than collection->[[Documentation:OCLOperators size|size]] | |||
somestring->[[Documentation:OCLOperators Contains|contains]]('a') is different than collection->[[Documentation:OCLOperators exists|exists]](x|x=vSomeObjectRef) | |||
somestring->[[Documentation:OCLOperators ToCharArray|ToCharArray]] actually turns the string into an array of chars | |||
==== Definition ==== | |||
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 ==== | |||
The syntax for declaring a String variable in OCL is straightforward. For example: | |||
variableName: String | variableName: String | ||
This declares a variable named `variableName` of type String. | This declares a variable named `variableName` of type String. | ||
==== Operations ==== | |||
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. | - Concatenation: Combining two strings together to form a new string. | ||
- Length: Determining the number of characters in a string. | - Length: Determining the number of characters in a string. | ||
Line 14: | Line 25: | ||
- Conversion: Converting a string to upper case, lower case, or other formats. | - Conversion: Converting a string to upper case, lower case, or other formats. | ||
==== Constraints ==== | |||
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 ==== | |||
- Concatenation: | - Concatenation: | ||
let str1: String = "Hello"; | let str1: String = "Hello"; | ||
Line 39: | Line 51: | ||
inv: name.size() >= 2 -- Ensures that the name of a person must be at least 2 characters long | 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. | |||
To insert a row break use '\r\n' | ==== Special string constants / String escape sequences ==== | ||
To insert a row break use <code>'\r\n'</code> | |||
To insert a tab use '\t' | To insert a tab use <code>'\t'</code> | ||
See this link [ | '''See this link:''' [https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters What are all the escape characters? - Stack Overflow] | ||
See also: [[Number conversions]] | '''See also:''' [[Documentation:Number conversions|Number conversions]] | ||
{{Edited|July|12| | {{Edited|July|12|2025}} | ||
[[Category:OCL]] | [[Category:OCL]] | ||
[[Category:Data types]] | [[Category:Data types]] | ||
[[Category:TOC]] |
Latest revision as of 06:00, 3 February 2025
Note
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
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
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
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
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
- 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
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