# [Ride v4 and v3] String functions
⚠️ This is the documentation for the Standard library version 4 and 3. We recommend to use version 6. Go to version 6
⚠️ Fixed an error in the implementation of string functions related to incorrect determination of UTF-8 character boundaries when splitting strings and searching for substrings in case of the strings contain special characters. The error could lead to invalid strings containing sequences that do not match any UTF-8 character. The fix is applied after activation of feature #16 “Ride V5, dApp-to-dApp invocations” regardless of the Standard library version.
| Name | Description | Complexity |
|---|---|---|
| contains(String, String): Boolean | Checks whether the string contains substring | 3 |
| drop(String, Int): String | Drops the first n characters of a string | 1 for Standard Library version 3 20 for Standard Library version 4 |
| dropRight(String, Int): String | Drops the last n characters of a string | 19 for Standard Library version 3 20 for Standard Library version 4 |
| indexOf(String, String): Int|Unit | Returns the index of the first occurrence of a substring | 20 for Standard Library version 3 3 for Standard Library version 4 |
| indexOf(String, String, Int): Int|Unit | Returns the index of the first occurrence of a substring after a certain index | 20 for Standard Library version 3 3 for Standard Library version 4 |
| lastIndexOf(String, String): Int|Unit | Returns the index of the last occurrence of a substring | 20 for Standard Library version 3 3 for Standard Library version 4 |
| lastindexOf(String, String, Int): Int|Unit | Returns the index of the last occurrence of a substring before a certain index | 20 for Standard Library version 3 3 for Standard Library version 4 |
| makeString(List[String], String): String | Concatenates list strings adding a separator | 30 |
| size(String): Int | Returns the size of a string | 1 |
| split(String, String): List[String] | Splits a string delimited by a separator into a list of substrings | 100 for Standard Library version 3 75 for Standard Library version 4 |
| take(String, Int): String | Takes the first n characters from a string | 1 for Standard Library version 3 20 for Standard Library version 4 |
| takeRight(String, Int): String | Takes the last n characters from a string | 19 for Standard Library version 3 20 for Standard Library version 4 |
# contains(String, String): Boolean
Checks whether the string contains substring.
⚠️ The
containsfunction is added in Standard library version 4.
contains(haystack: String, needle: String): Boolean
# Parameters
| Parameter | Description |
|---|---|
haystack: String | String to search in |
needle: String | String to search for |
# Examples
{-# STDLIB_VERSION 4 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
"hello".contains("hell") # Returns true
"hello".contains("world") # Returns false
# drop(String, Int): String
Drops the first n characters of a string.
drop(xs: String, number: Int): String
# Parameters
| Parameter | Description |
|---|---|
xs: String | The string |
number: Int | The number n |
# Examples
drop("Apple", 0) # Returns "Apple"
drop("Apple", 1) # Returns "pple"
drop("Apple", 3) # Returns "le"
drop("Apple", 5) # Returns an empty string
drop("Apple", 15) # Returns an empty string
# dropRight(String, Int): String
Drops the last n characters of a string.
dropRight(xs: String, number: Int): String
# Parameters
| Parameter | Description |
|---|---|
xs: String | The string |
number: Int | The number n |
# Examples
dropRight("Apple", 0) # Returns "Apple"
dropRight("Apple", 1) # Returns "Appl"
dropRight("Apple", 3) # Returns "Ap"
dropRight("Apple", 5) # Returns an empty string
dropRight("Apple", 15) # Returns an empty string
# indexOf(String, String): Int|Unit
Returns the index of the first occurrence of a substring.
indexOf(str: String, substr: String): Int|Unit
# Parameters
| Parameter | Description |
|---|---|
str: String | The string |
substr: String | The substring |
# Examples
indexOf("Apple","ple") # Returns 2
indexOf("Apple","le") # Returns 3
indexOf("Apple","e") # Returns 4
# indexOf(String, String, Int): Int|Unit
Returns the index of the first occurrence of a substring after a certain index.
indexOf(str: String, substr: String, offset: Int): Int|Unit
# Parameters
| Parameter | Description |
|---|---|
str: String | The string |
substr: String | The substring |
offset: Int | The index |
# Examples
indexOf("Apple","ple", 1) # Returns 2
indexOf("Apple","le", 2) # Returns 3
indexOf("Apple","e", 3) # Returns 4
indexOf("Apple","p", 0) # Returns 1
indexOf("Apple","p", 2) # Returns 2
indexOf("Apple","p", 3) # Returns unit
# lastIndexOf(String, String): Int|Unit
Returns the index of the last occurrence of a substring.
lastIndexOf(str: String, substr: String): Int|Unit
# Parameters
| Parameter | Description |
|---|---|
str: String | The string |
substr: String | The substring |
# Examples
lastIndexOf("Apple","pp") # Returns 1
lastIndexOf("Apple","p") # Returns 2
lastIndexOf("Apple","s") # Returns unit
# lastIndexOf(String, String, Int): Int|Unit
Returns the index of the last occurrence of a substring before a certain index.
lastIndexOf(str: String, substr: String, offset: Int): Int|Unit
# Parameters
| Parameter | Description |
|---|---|
str: String | The string |
substr: String | The substring |
offset: Int | The index |
# Examples
lastIndexOf("mamamama","ma",4) # Returns 4
lastIndexOf("mamamama","ma",3) # Returns 2
# makeString(List[String], String): String
Concatenates list strings adding a separator.
⚠️ The
makeStringfunction is added in Standard library version 4.
makeString(arr: List[String], separator: String): String
# Parameters
| Parameter | Description |
|---|---|
arr: List[String] | List of strings to concatenate |
separator: String | Separator |
# Example
makeString(["Apple","Orange","Mango"], " & ") # Returns "Apple & Orange & Mango"
# size(String): Int
Returns the size of a string.
size(xs: String): Int
# Parameters
| Parameter | Description |
|---|---|
xs: String | The string |
# Examples
size("Ap") # Returns 2
size("Appl") # Returns 4
size("Apple") # Returns 5
# split(String, String): List[String]
Splits a string delimited by a separator into a list of substrings.
split(str: String, separator: String): List[String]
# Parameters
| Parameter | Description |
|---|---|
str: String | The string |
separator: Int | The separator |
# Examples
split("A.p.p.l.e", ".") # Returns ["A", "p", "p", "l", "e"]
split("Apple", ".") # Returns ["Apple"]
split("Apple", "") # Returns ["A", "p", "p", "l", "e"]
split("Ap.ple", ".") # Returns ["Ap","ple"]
# take(String, Int): String
Takes the first n characters from a string.
take(xs: String, number: Int): String
# Parameters
| Parameter | Description |
|---|---|
xs: String | The string |
number: Int | The number n |
# Examples
take("Apple", 0) # Returns an empty string
take("Apple", 1) # Returns "A"
take("Apple", 3) # Returns "App"
take("Apple", 5) # Returns "Apple"
take("Apple", 15) # Returns "Apple"
take("Apple", -10) # Returns an empty string
# takeRight(String, Int): String
Takes the last n characters from a string.
takeRight(xs: String, number: Int): String
# Parameters
| Parameter | Description |
|---|---|
xs: String | The string |
number: Int | The number n |
# Examples
takeRight("Apple", 0) # Returns an empty string
takeRight("Apple", 1) # Returns "A"
takeRight("Apple", 3) # Returns "ple"
takeRight("Apple", 5) # Returns "Apple"
takeRight("Apple", 15) # Returns "Apple"