String operators

The String operators can be used to combine values of type String and are the following:

substring

returns a substring of a given string using zero-based indexes denoting the start and the end of the substring

contains

returns true if a given string contains a specified substring

starts_with

returns true if the string starts with a specified prefix

ends_with

returns true if the string ends with a specified suffix

concat

returns the concatenation of two strings

+ (plus symbol)

works as string concatenation if any of the two operands is a string

index_of

returns the index of the first occurrence of a specified substring within a string

string_length

returns the length of a given string

substring

A rule with an assignment with a substring operator has the general form and returns the substring using a zero-based index. start and end represent integers, respectively the start and the end of the substring returned.

substring(String, Start, End)

Where:

  • String is the original string.

  • Start is the zero-based starting index (inclusive).

  • End is the zero-based ending index (exclusive).

Example
a("vadaengine").
b("oxford").
q(Y, J) :- a(X), b(Y), J = substring(X, 4, 10).
@output("q").
Expected results
q("oxford", "engine")

contains

A rule with an assignment using the contains operator returns true if the string contains search_string.

contains(String, SearchString)

Where:

  • String is the original string.

  • SearchString is the string to search for within the original string.

Example
a("vadaengine").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = contains(X, Y).
@output("q").
Expected results
q("vadaengine", "engine", #T)

starts_with

A rule with an assignment using the starts_with operator returns true if the string starts with start_string.

starts_with(String, StartString)

Where:

  • String is the original string.

  • StartString is the string to check as the starting substring of the original string.

Example
a("vadaengine").
b("vada").
q(X, Y, J) :- a(X), b(Y), J = starts_with(X, Y).
@output("q").
Expected results
q("vadaengine", "vada", #T)

ends_with

A rule with an assignment using the ends_with operator returns true if the string ends with end_string.

ends_with(String, EndString)

Where:

  • String is the original string.

  • EndString is the string to check as the ending substring of the original string.

Example
a("vadaengine").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = ends_with(X, Y).
@output("q").
Expected results
q("vadaengine", "engine", #T)

concat

A rule with an assignment using the concat operator returns the concatenation of string and concat_string.

concat(String, ConcatString)

Where:

  • String is the first string.

  • ConcatString is the second string to be concatenated.

Example
a("vada").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = concat(X, Y).
@output("q").
Expected results
q("vada", "engine", "vadaengine")

+ (plus symbol)

Similarly, the operation + works as concat. Any operands other than string are upcasted to string, i.e., toString is performed.

String + ConcatString

Where:

  • String is the first string.

  • ConcatString is the second string or element to be concatenated.

Example
a("vada").
b(1.0).
q(X, Y, J) :- a(X), b(Y), J = X + Y.
@output("q").
Expected results
q("vada", 1.0, "vada1.0")

index_of

A rule with an assignment using the index_of operator returns an integer representing the string index of the occurrence index_string.

index_of(String, IndexString)

Where:

  • String is the original string.

  • IndexString is the string to find the index of within the original string.

Example
a("vadaengine").
b("engi").
q(X, Y, J) :- a(X), b(Y), J = index_of(X, Y).
@output("q").
Expected results
q("vadaengine", "engi", 4)

string_length

A rule with an assignment using the string_length operator returns an integer representing the length of the string.

string_length(String)

Where:

  • String is the original string.

Example
a("vadaengine").
q(X, J) :- a(X), J = string_length(X).
@output("q").
Expected results
q("vadaengine", 10)