String module

This module contains types and functions to manipulate and build strings. LocalSolver is compliant with Unicode 10.0. Thus, operations like computing the length of a string, transforming a string to uppercase or lowercase will be correctly handled by the modeler for all characters of all languages supported by Unicode 10.0.

Note: If you plan to use non-ASCII characters in your source code, you should set the encoding of your LSP files accordingly, otherwise characters could be misinterpreted and the functions of this module will not work correctly. By default, the parser assumes an ISO-8859-1 (Latin1) encoding. It can also detect UTF-8 or UTF-16 encoding automatically if a Byte Order Mark (BOM) is present. Please read Encoding to see how to change the character set or the encoding of your LSP files.

This note does not apply if you only want to manipulate non-ASCII characters (by reading or writing files for example), without using them in your source code.

type string

This is the base type for strings. String objects are compliant with Unicode 10.0 and can contain any unicode character.

toInt()

Returns an integer representation of this string object. If this string does not represent an integer or if the conversion is not possible (out of range), an exception is thrown.

toDouble()

Returns a float representation of this string object. If this string does not represent a float or if the conversion is not possible (out of range), an exception is thrown.

split()
split(delimiter)

Returns a map that contains the substrings of this string object that are delimited by the given delimiter. For example "a::b::::c".split("::") returns {"a", "b", "", "c"}.

If no delimiter is provided, whitespaces are used as delimiter. In that case, consecutive whitespaces are considered as a single separator. Thus a string composed of whitespaces only, always returns an empty map {}. See isWhitespace() for the exact definition of a whitespace.

Splitting an empty string with or without a delimiter always returns an empty map {}.

Parameters:delimiter (string) – Delimiter used to split the string. This parameter is optionnal. If no delimiter is provided, whitespaces are used by default.
Returns:A map containing the splitted substrings
Return type:map
trim()

Returns a copy of the string with leading and trailing whitespaces removed. The list of whitespaces is the same as the list used for the function isWhitespace().

length()

Returns the length of the string as integer.

startsWith(prefix)

Returns true if the string starts with the given prefix. If the prefix is an empty string, returns true.

Parameters:prefix (string) – The prefix.
endsWith(suffix)

Returns true if the string ends with the given suffix. If the suffix is an empty string, returns true.

Parameters:prefix (string) – The suffix.
toLowerCase()

Returns a new string with all the characters converted to lower case.

toUpperCase()

Returns a new string with all the characters converted to upper case.

replace(search, replace)

Return a copy of the string with all occurrences of substring search replaced by replace.

Parameters:
  • search (string) – Search string. Throws an exception if this argument is empty.
  • replace (string) – Replace string.
string.isLower()

Returns true if the string contains only characters that are lowercase and there is at least one character, false otherwise. A character is lowercase only if its unicode general category is “Letter, Lowercase” (Ll).

isUpper()

Returns true if the string contains only characters that are uppercase and there is at least one character, false otherwise. A character is uppercase only if its unicode general category is “Letter, Uppercase” (Lu).

isLetter()

Returns true if the string contains only letters and there is at least one character, false otherwise. A character is a letter only if its unicode general category is any of the following:

  • Letter, Uppercase (Lu)
  • Letter, Lowercase (Ll)
  • Letter, Titlecase (Lt)
  • Letter, Modifier (Lm)
  • Letter, Other (Lo)
isDigit()

Returns true if the string contains only digits and there is at least one character, false otherwise. A character is a digit only it its unicode general category is “Number, Digit” (Nd). “0123456789” are examples of digits.

isSpace()

Returns true if the string contains only spaces and there is at least one character, false otherwise. A character is a space only if its unicode general category is any of the following:

  • Separator, Space (Zs)
  • Separator, Line (Zl)
  • Separator, Paragraph (Zp)

The definitions of “spaces” and “whitespaces” are quite similar, and a space is also a whitespace. But whitespaces also include special control characters like “\n” or “\t” that are not labeled as “spaces” by the unicode consortium. Read isWhitespace() for more details.

isWhitespace()

Returns true if the string contains only whitespaces and there is at least one character, false otherwise. A character is a space only if it has the property “WSpace=Y” in the unicode database. The complete list of whitespaces contains:

  • All characters with general category “Separator, Space” (Zs)
  • All characters with general category “Separator, Line” (Zl)
  • All characters with general category “Separator, Paragraph” (Zp)
  • The control character “Horizontal tabulation” (U+0009)
  • The control character “Line feed” (U+000A)
  • The control character “Vertical tabulation” (U+000B)
  • The control character “Form feed” (U+000C)
  • The control character “Carriage return” (U+000D)
  • The control character “Next line” (U+0085)
indexOf(search)
indexOf(search, start)

Returns the first position of the substring search in the string. If no start position is given, the search starts at the beginning of the string. Returns -1 if the substring is not found.

Parameters:
  • search (string) – Substring to search
  • start (int) – The position to start the search from. If the start position is negative, an exception is thrown.
substring(start)
substring(start, length)

Returns a new string that is a substring of this string. There are two versions of this function:

  • In the first version, the function takes one argument. In that case, the returned substring starts at the given position and goes to the end of the string.
  • In the second version, the returned substring starts at the given position and goes to the character at position start + length - 1.
Parameters:
  • start (int) – Start position. If the start position is negative or greater than the length of the string, an exception is thrown.
  • length (int) – Length of the substring (optionnal). If the given length is negative, an exception is thrown.