Package Strings contains functions to manipulate strings.
In the table below an example call to every function is given using the default options.
Function | Description |
---|---|
len = length(string) | Returns length of string |
string2 = substring(string1,startIndex,endIndex) | Returns a substring defined by start and end index |
result = repeat(n) result = repeat(n,string) |
Repeat a blank or a string n times. |
result = compare(string1, string2) | Compares two substrings with regards to alphabetical order |
identical = isEqual(string1,string2) | Determine whether two strings are identical |
result = count(string,searchString) | Count the number of occurrences of a string |
index = find(string,searchString) | Find first occurrence of a string in another string |
index = findLast(string,searchString) | Find last occurrence of a string in another string |
string2 = replace(string,searchString,replaceString) | Replace one or all occurrences of a string |
stringVector2 = sort(stringVector1) | Sort vector of strings in alphabetic order |
hash = hashString(string) | Create a hash value of a string |
(token, index) = scanToken(string,startIndex) | Scan for a token (Real/Integer/Boolean/String/Identifier/Delimiter/NoToken) |
(number, index) = scanReal(string,startIndex) | Scan for a Real constant |
(number, index) = scanInteger(string,startIndex) | Scan for an Integer constant |
(boolean, index) = scanBoolean(string,startIndex) | Scan for a Boolean constant |
(string2, index) = scanString(string,startIndex) | Scan for a String constant |
(identifier, index) = scanIdentifier(string,startIndex) | Scan for an identifier |
(delimiter, index) = scanDelimiter(string,startIndex) | Scan for delimiters |
scanNoToken(string,startIndex) | Check that remaining part of string consists solely of white space or line comments ("// ...\n"). |
syntaxError(string,index,message) | Print a "syntax error message" as well as a string and the index at which scanning detected an error |
The functions "compare", "isEqual", "count", "find", "findLast", "replace", "sort" have the optional input argument caseSensitive with default true. If false, the operation is carried out without taking into account whether a character is upper or lower case.
Extends from Modelica.Icons.FunctionsPackage
(Icon for packages containing functions).
Name | Description |
---|---|
Advanced … | Advanced scanning functions |
compare | Compare two strings lexicographically |
count | Count the number of non-overlapping occurrences of a string |
find | Find first occurrence of a string within another string |
findLast | Find last occurrence of a string within another string |
hashString | Create a hash value of a string |
isEmpty | Return true if a string is empty (has only white space characters) |
isEqual | Determine whether two strings are identical |
length | Return length of string |
repeat | Repeat a string n times |
replace | Replace non-overlapping occurrences of a string from left to right |
scanBoolean | Scan for the next Boolean number and trigger an assert if not present |
scanDelimiter | Scan for the next delimiter and trigger an assert if not present |
scanIdentifier | Scan for the next Identifier and trigger an assert if not present |
scanInteger | Scan for the next Integer number and trigger an assert if not present |
scanNoToken | Scan string and check that it contains no more token |
scanReal | Scan for the next Real number and trigger an assert if not present |
scanString | Scan for the next Modelica string and trigger an assert if not present |
scanToken | Scan for the next token and return it |
sort | Sort vector of strings in alphabetic order |
substring | Return a substring defined by start and end index |
syntaxError | Print an error message, a string and the index at which scanning detected an error |
Strings.length(string);
Returns the number of characters of "string".
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string |   |
Type | Name | Description |
---|---|---|
Integer | result | Number of characters of string |
string2 = Strings.substring(string, startIndex, endIndex);
This function returns the substring from position startIndex up to and including position endIndex of "string" .
If index, startIndex, or endIndex are not correct, e.g., if endIndex > length(string), an assert is triggered.
string1 := "This is line 111"; string2 := Strings.substring(string1,9,12); // string2 = "line"
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String from which a substring is inquired |
Integer | startIndex | Character position of substring begin (index=1 is first character in string) |
Integer | endIndex | Character position of substring end |
Type | Name | Description |
---|---|---|
String | result | String containing substring string[startIndex:endIndex] |
string2 = Strings.repeat(n); string2 = Strings.repeat(n, string=" ");
The first form returns a string consisting of n blanks.
The second form returns a string consisting of n substrings defined by the optional argument "string".
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
Integer | n | Number of occurrences |
String | string | String that is repeated |
Type | Name | Description |
---|---|---|
String | repeatedString | String containing n concatenated strings |
result = Strings.compare(string1, string2); result = Strings.compare(string1, string2, caseSensitive=true);
Compares two strings. If the optional argument caseSensitive=false, upper case letters are treated as if they would be lower case letters. The result of the comparison is returned as:
result = Modelica.Utilities.Types.Compare.Less // string1 < string2 = Modelica.Utilities.Types.Compare.Equal // string1 = string2 = Modelica.Utilities.Types.Compare.Greater // string1 > string2
Comparison is with regards to lexicographical order, e.g., "a" < "b";
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string1 |   |
String | string2 |   |
Boolean | caseSensitive | = false, if case of letters is ignored |
Type | Name | Description |
---|---|---|
Compare | result | Result of comparison |
Strings.isEqual(string1, string2); Strings.isEqual(string1, string2, caseSensitive=true);
Compare whether two strings are identical, optionally ignoring case.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string1 |   |
String | string2 |   |
Boolean | caseSensitive | = false, if lower and upper case are ignored for the comparison |
Type | Name | Description |
---|---|---|
Boolean | identical | True, if string1 is identical to string2 |
Strings.isEmpty(string);
Returns true if the string has no characters or if the string consists only of white space characters. Otherwise, false is returned.
isEmpty(""); // returns true isEmpty(" "); // returns true isEmpty(" abc"); // returns false isEmpty("a"); // returns false
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string |   |
Type | Name | Description |
---|---|---|
Boolean | result | True, if string is empty |
Strings.count(string, searchString) Strings.count(string, searchString, startIndex=1, caseSensitive=true)
Returns the number of non-overlapping occurrences of string "searchString" in "string". The search is started at index "startIndex" (default = 1). If the optional argument "caseSensitive" is false, for the counting it does not matter whether a letter is upper or lower case.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String that is analyzed |
String | searchString | String that is searched for in string |
Integer | startIndex | Start search at index startIndex |
Boolean | caseSensitive | = false, if lower and upper case are ignored for count |
Type | Name | Description |
---|---|---|
Integer | result | Number of occurrences of 'searchString' in 'string' |
index = Strings.find(string, searchString); index = Strings.find(string, searchString, startIndex=1, caseSensitive=true);
Finds first occurrence of "searchString" within "string" and return the corresponding index. Start search at index "startIndex" (default = 1). If the optional argument "caseSensitive" is false, lower and upper case are ignored for the search. If "searchString" is not found, a value of "0" is returned.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String that is analyzed |
String | searchString | String that is searched for in string |
Integer | startIndex | Start search at index startIndex |
Boolean | caseSensitive | = false, if lower and upper case are ignored for the search |
Type | Name | Description |
---|---|---|
Integer | index | Index of the beginning of the first occurrence of 'searchString' within 'string', or zero if not present |
index = Strings.findLast(string, searchString); index = Strings.findLast(string, searchString, startIndex=length(string), caseSensitive=true,
Finds first occurrence of "searchString" within "string" when searching from the last character of "string" backwards, and return the corresponding index. Start search at index "startIndex" (default = 0; if startIndex = 0, search starts at length(string)). If the optional argument "caseSensitive" is false, lower and upper case are ignored for the search. If "searchString" is not found, a value of "0" is returned.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String that is analyzed |
String | searchString | String that is searched for in string |
Integer | startIndex | Start search at index startIndex. If startIndex = 0, start at length(string) |
Boolean | caseSensitive | = false, if lower and upper case are ignored for the search |
Type | Name | Description |
---|---|---|
Integer | index | Index of the beginning of the last occurrence of 'searchString' within 'string', or zero if not present |
Strings.replace(string, searchString, replaceString); Strings.replace(string, searchString, replaceString, startIndex=1, replaceAll=true, caseSensitive=true);
Search in "string" for "searchString" and replace the found substring by "replaceString".
The function returns the "string" with the performed replacements.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be modified |
String | searchString | Replace non-overlapping occurrences of 'searchString' in 'string' with 'replaceString' |
String | replaceString | String that replaces 'searchString' in 'string' |
Integer | startIndex | Start search at index startIndex |
Boolean | replaceAll | if false, replace only the first occurrence, otherwise all occurrences |
Boolean | caseSensitive | = false, if lower and upper case are ignored when searching for searchString |
Type | Name | Description |
---|---|---|
String | result | Resultant string of replacement operation |
stringVector2 = Streams.sort(stringVector1); stringVector2 = Streams.sort(stringVector1, caseSensitive=true);
Function sort(..) sorts a string vector stringVector1 in lexicographical order and returns the result in stringVector2. If the optional argument "caseSensitive" is false, lower and upper case letters are not distinguished.
s1 = {"force", "angle", "pressure"}; s2 = Strings.sort(s1); -> s2 = {"angle", "force", "pressure"};
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | stringVector1[:] | vector of strings |
Boolean | caseSensitive | = false, if lower and upper case are ignored when comparing elements of stringVector1 |
Type | Name | Description |
---|---|---|
String | stringVector2[size(stringVector1, 1)] | stringVector1 sorted in alphabetical order |
hash = Strings.hashString(string);
Returns an Integer hash value of the provided string (the hash can be any Integer, including zero or a negative number).
hashString("this is a test") // = 1827717433 hashString("Controller.noise1") // = -1025762750
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | The string to create a hash from |
Type | Name | Description |
---|---|---|
Integer | hash | The hash value of string |
(token, nextIndex) = Strings.scanToken(string, startIndex, unsigned=false);
Function scanToken scans the string starting at index "startIndex" and returns the next token, as well as the index directly after the token. The returned token is a record that holds the type of the token and the value of the token:
token.tokenType | Type of the token, see below |
token.real | Real value if tokenType == TokenType.RealToken |
token.integer | Integer value if tokenType == TokenType.IntegerToken |
token.boolean | Boolean value if tokenType == TokenType.BooleanToken |
token.string | String value if tokenType == TokenType.StringToken/IdentifierToken/DelimiterToken |
Variable token.tokenType is an enumeration (emulated as a package with constants) that can have the following values:
import T = Modelica.Utilities.Types.TokenType;
T.RealToken | Modelica Real literal (e.g., 1.23e-4) |
T.IntegerToken | Modelica Integer literal (e.g., 123) |
T.BooleanToken | Modelica Boolean literal (e.g., false) |
T.StringToken | Modelica String literal (e.g., "string 123") |
T.IdentifierToken | Modelica identifier (e.g., "force_a") |
T.DelimiterToken | any character without white space that does not appear as first character in the tokens above (e.g., "&") |
T.NoToken | White space, line comments and no other token until the end of the string |
Modelica line comments ("// ... end-of-line/end-of-string") as well as white space is ignored. If "unsigned=true", a Real or Integer literal is not allowed to start with a "+" or "-" sign.
import Modelica.Utilities.Strings; import T = Modelica.Utilities.Types.TokenType; (token, index) := Strings.scanToken(string); if token.tokenType == T.RealToken then realValue := token.real; elseif token.tokenType == T.IntegerToken then integerValue := token.integer; elseif token.tokenType == T.BooleanToken then booleanValue := token.boolean; elseif token.tokenType == T.Identifier then name := token.string; else Strings.syntaxError(string,index,"Expected Real, Integer, Boolean or identifier token"); end if;
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
Boolean | unsigned | = true, if Real and Integer tokens shall not start with a sign |
Type | Name | Description |
---|---|---|
TokenValue | token | Scanned token |
Integer | nextIndex | Index of character after the found token; = 0, if NoToken |
number = Strings.scanReal(string); (number, nextIndex) = Strings.scanReal(string, startIndex=1, unsigned=false, message="");
The first form, "scanReal(string)", scans "string" for a Real number with leading white space and returns the value.
The second form, "scanReal(string,startIndex,unsigned)", scans the string starting at index "startIndex", checks whether the next token is a Real literal and returns its value as a Real number, as well as the index directly after the Real number. If the optional argument "unsigned" is true, the real number shall not have a leading "+" or "-" sign.
If the required Real number with leading white space is not present in "string", an assert is triggered.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
Boolean | unsigned | = true, if Real token shall not start with a sign |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
Real | number | Value of real number |
Integer | nextIndex | index of character after the found number |
number = Strings.scanInteger(string); (number, nextIndex) = Strings.scanInteger(string, startIndex=1, unsigned=false, message="");
Function scanInteger scans the string starting at index "startIndex", checks whether the next token is an Integer literal and returns its value as an Integer number, as well as the index directly after the Integer number. An assert is triggered, if the scanned string does not contain an Integer literal with optional leading white space.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
Boolean | unsigned | = true, if Integer token shall not start with a sign |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
Integer | number | Value of Integer number |
Integer | nextIndex | Index of character after the found number |
number = Strings.scanBoolean(string); (number, nextIndex) = Strings.scanBoolean(string, startIndex=1, message="");
Function scanBoolean scans the string starting at index "startIndex", checks whether the next token is a Boolean literal (i.e., is either the string "false" or "true", if converted to lower case letters) and returns its value as a Boolean number, as well as the index directly after the Boolean number. An assert is triggered, if the scanned string does not contain a Boolean literal with optional leading white space.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
Boolean | number | Value of Boolean |
Integer | nextIndex | Index of character after the found number |
string2 = Strings.scanString(string); (string2, nextIndex) = Strings.scanString(string, startIndex=1, message="");
Function scanString scans the string starting at index "startIndex", checks whether the next token is a String literal and returns its value as a String, as well as the index directly after the String. An assert is triggered, if the scanned string does not contain a String literal with optional leading white space.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
String | result | Value of string |
Integer | nextIndex | Index of character after the found string |
identifier = Strings.scanIdentifier(string); (identifier, nextIndex) = Strings.scanIdentifier(string, startIndex=1, message="");
Function scanIdentifier scans the string starting at index "startIndex", checks whether the next token is an Identifier and returns its value as a string, as well as the index directly after the Identifier. An assert is triggered, if the scanned string does not contain an Identifier with optional leading white space.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of identifier at character startIndex |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
String | identifier | Value of Identifier |
Integer | nextIndex | Index of character after the found identifier |
delimiter = Strings.scanDelimiter(string); (delimiter, nextIndex) = Strings.scanDelimiter(string, startIndex=1, requiredDelimiters={","}, message="");
Function scanDelimiter scans the string starting at index "startIndex", checks whether the next token is a delimiter string and returns its value as a string, as well as the index directly after the delimiter. An assert is triggered, if the scanned string does not contain a delimiter out of the list of requiredDelimiters. Input argument requiredDelimiters is a vector of strings. The elements may have any length, including length 0. If an element of the requiredDelimiters is zero, white space is treated as delimiter. The function returns delimiter="" and nextIndex is the index of the first non white space character.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of delimiters at character startIndex |
String | requiredDelimiters[:] | Delimiters that are searched |
String | message | Message used in error message if scan is not successful |
Type | Name | Description |
---|---|---|
String | delimiter | Found delimiter |
Integer | nextIndex | Index of character after the found delimiter |
Strings.scanNoToken(string, startIndex=1, message="");
Function scanNoToken scans the string starting at index "startIndex" and checks whether there is no more token in the string. An assert is triggered if this is not the case, using the "message" argument as additional explanation in the error text.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String to be scanned |
Integer | startIndex | Start scanning of string at character startIndex |
String | message | Message used in error message if scan is not successful |
Strings.syntaxError(string, index, message);
Function syntaxError prints an error message in the following form:
Syntax error at column <index> of <string> ^ // shows character that is wrong <message>
where the strings within <..> are the actual values of the input arguments of the function.
If the given string is too long, only a relevant part of the string is printed.
Extends from Modelica.Icons.Function
(Icon for functions).
Type | Name | Description |
---|---|---|
String | string | String that has an error at position index |
Integer | index | Index of string at which scanning detected an error |
String | message | String printed at end of error message |