String operators
The String operators can be used to combine values of type String and are the following:
returns a substring of a given string using zero-based indexes denoting the start and the end of the substring |
|
returns true if a given string contains a specified substring |
|
returns true if the string starts with a specified prefix |
|
returns true if the string ends with a specified suffix |
|
returns the concatenation of two strings |
|
works as string concatenation if any of the two operands is a string |
|
returns the index of the first occurrence of a specified substring within a string |
|
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).
a("vadaengine").
b("oxford").
q(Y, J) :- a(X), b(Y), J = substring(X, 4, 10).
@output("q").
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.
a("vadaengine").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = contains(X, Y).
@output("q").
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.
a("vadaengine").
b("vada").
q(X, Y, J) :- a(X), b(Y), J = starts_with(X, Y).
@output("q").
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.
a("vadaengine").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = ends_with(X, Y).
@output("q").
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.
a("vada").
b("engine").
q(X, Y, J) :- a(X), b(Y), J = concat(X, Y).
@output("q").
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.
a("vada").
b(1.0).
q(X, Y, J) :- a(X), b(Y), J = X + Y.
@output("q").
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.
a("vadaengine").
b("engi").
q(X, Y, J) :- a(X), b(Y), J = index_of(X, Y).
@output("q").
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.
a("vadaengine").
q(X, J) :- a(X), J = string_length(X).
@output("q").
q("vadaengine", 10)