Ludwig.String

Module Members

concat

(Function)

Concatenate two strings.

Example:

x: String.concat("hello", "world") # => "helloworld"

Type Signature

 fun (String, String) -> String
Argument: a
Type: String
Argument: b
Type: String
Returns:
Type: String

drop

(Function)

Return the suffix of a String.

Example usage:

ex1: String.drop(3, "foobar") # => "bar"
ex2: String.drop(4, "foo")    # => ""

Using String.drop(n, s) is equivalent to using String.slice {start: n, string: s}.

Type Signature

 fun (Int, String) -> String
Argument: n

The length of the prefix to remove.

Type: Int

Argument: str

The String to operate on.

Type: String

Returns:

A String made up of str with the first n characters removed.

Type: String

endsWith

(Function)

Test if a String ends with a given suffix. Example: ex1: String.endsWith(“foo”, “helloworld”) # => False ex2: String.endsWith(“world”, “helloworld”) # => True

Type Signature

 fun (String, String) -> Bool
Argument: suffix

The suffix.

Type: String

Argument: str

The String to test.

Type: String

Returns:

True is str ends with suffix; False otherwise.

Type: Bool

equal

(Function)

Test if two Strings are equal.

This is the same as using the == operator.

Example:

x1: String.equal("foo", "bar") # => False
x2: String.equal("foo", "foo") # => True

Type Signature

 fun (String, String) -> Bool
Argument: x
Type: String
Argument: y
Type: String
Returns:

True if the two Strings are equal, False otherwise.

Type: Bool

getEnv

(Function)

Get an environment variable. Returns None if the environment variable is not set.

Example:

path: String.getEnv("PATH")

Type Signature

 fun (String) -> Optional<String>
Argument: varName

Name of the environment variable to get.

Type: String

Returns:

The value of that environment variable, or None.

Type: Optional<String>

getEnvOrDefault

(Function)

Get an environment variable. If the environment variable is not set, use the provided default.

Example:

path: String.getEnvOrDefault("PATH", "/usr/bin:/bin")

Type Signature

 fun (String, String) -> String
Argument: varName

Name of the environment variable to get.

Type: String

Argument: def

The value to use if the environment variable is not set.

Type: String

Returns:

The value of that environment variable, or the default.

Type: String

getEnvOrError

(Function)

Get an environment variable. If the environment variable is not set, throw an error message.

Example:

path: String.getEnvOrError("PATH")

Type Signature

 fun (String) -> String
Argument: varName

Name of the environment variable to get.

Type: String

Returns:

The value of that environment variable.

Type: String

isEmpty

(Function)

Test if a Ludwig.String is empty.

Example:

x1: String.isEmpty("")    # => True
x2: String.isEmpty("foo") # => False

Type Signature

 fun (String) -> Bool
Argument: s

The Ludwig.String to test.

Type: String

Returns:

True if s is empty, False otherwise.

Type: Bool

join

(Function)

Insert separator String in between elements in the list and concatenate the result.

Example: ex1: String.join(”:”, [“a”, “b”, “c”]) # => “a:b:c”

Type Signature

 fun (String, List<String>) -> String
Argument: sep

The separator.

Type: String

Argument: xs

The list of input Strings

Type: List<String>

Returns:
Type: String

length

(Function)

Returns the length of the given ‘String’

Example:

x: String.length("foo") # => 3

Type Signature

 fun (String) -> Int
Argument: string
Type: String
Returns:
Type: Int

null

(Function)

DEPRECATED: Use isEmpty instead.

Test if a Ludwig.String is empty.

Example:

x1: String.null("")    # => True
x2: String.null("foo") # => False

Type Signature

 fun (String) -> Bool
Argument: s

The Ludwig.String to test.

Type: String

Returns:

True if s is empty, False otherwise.

Type: Bool

print

(Function)

DEPRECATED: Use trace instead. Print a string to stdout.

Example:

x: String.print("Hello world!\n")

Type Signature

 fun (String) -> 0-tuple
Argument: str

The string to print.

Type: String

Returns:
Type: 0-tuple

readFileUtf8

(Function)

Read a Ludwig.String from an UTF-8 encoded file.

Example:

script: String.readFileUtf8("etc/script.sh")

Type Signature

 fun (String) -> String
Argument: filePath

The path of the file to read. Relative paths are considered relative to the directory where the compiler is invoked, not the location of the composition.

Type: String

Returns:

The contents of the file.

Type: String

slice

(Function)

Take a substring.

Example:

x01: String.slice {start: 2, list: "Hello"} # => "llo"

This function will never throw an error. If start is beyond the length of the original list, the empty string will be returned. If length is too long, the string will be trimmed so it still returns as many characters as possible.

Type Signature

 fun { start: Optional<Int>,
       length: Optional<Int>,
       string: String} -> String
Argument: start
Type: Optional<Int>
Argument: length
Type: Optional<Int>
Argument: string

The string to slice. @result The requested substring.

Type: String

Returns:
Type: String

split

(Function)

Split a Ludwig.String on occurrences of a separator.

Example:

split1: String.split("/", "1/2/3")   # => ["1", "2", "3"]
split2: String.split("", "hello")    # => ["h", "e", "l", "l", "o"]

If you want to split using a regular expression instead, take a look at the Ludwig.Regex module.

Type Signature

 fun (String, String) -> List<String>
Argument: separator

The separator to split the source Ludwig.String on.

Type: String

Argument: source

The Ludwig.String to split.

Type: String

Returns:

The Ludwig.List of sub-strings. Ludwig.List of characters if the separator is the empty string.

Type: List<String>

startsWith

(Function)

Test if a String starts with a given prefix.

Example:

ex1: String.startsWith("foo", "helloworld")   # => False
ex2: String.startsWith("hello", "helloworld") # => True

Type Signature

 fun (String, String) -> Bool
Argument: prefix

The prefix.

Type: String

Argument: str

The String to test.

Type: String

Returns:

True is str begins with prefix; False otherwise.

Type: Bool

take

(Function)

Return a prefix of a String.

Example usage:

ex1: String.take(3, "hello") # => "hel"
ex2: String.take(4, "foo")   # => "foo"

Using String.take(n, s) is equivalent to using String.slice {length: n, string: s}.

Type Signature

 fun (Int, String) -> String
Argument: n

The length of the prefix to return.

Type: Int

Argument: str

The String to operate on.

Type: String

Returns:

A String made up of the first n characters from str.

Type: String

toBytes

(Function)

Convert the provided Ludwig.String to Ludwig.Bytes.

Example:

ex1: String.toBytes(“hello world”)

Type Signature

 fun (String) -> Bytes
Argument: str

The string to convert.

Type: String

Returns:

The string converted to UTF-8 encoded bytes.

Type: Bytes

toFloat

(Function)

Convert the provided Ludwig.String to a Ludwig.Float.

Example:

x1: String.toFloat("42")           # => Optional(42.0)
x2: String.toFloat("-1")           # => Optional(-1.0)
x3: String.toFloat("3.14")         # => Optional(3.14)
x4: String.toFloat("3 blind mice") # => None
x5: String.toFloat("hello")        # => None

Type Signature

 fun (String) -> Optional<Float>
Argument: s

The Ludwig.String to convert.

Type: String

Returns:

An Ludwig.Optional with the converted Ludwig.Float or None if s cannot be converted.

Type: Optional<Float>

toInt

(Function)

Convert the provided Ludwig.String to a Ludwig.Int

Example:

x1: String.toInt("42")           # => Optional(42)
x2: String.toInt("-1")           # => Optional(-1)
x3: String.toInt("3.14")         # => None
x4: String.toInt("3 blind mice") # => None
x5: String.toInt("hello")        # => None

Type Signature

 fun (String) -> Optional<Int>
Argument: s

The Ludwig.String to convert.

Type: String

Returns:

An Ludwig.Optional with the converted Ludwig.Int or None if s cannot be converted.

Type: Optional<Int>


(Operator)

operator ++ infixr 5:
  concat

String concatenation.

Example:

x: "hello" ++ "world" # => "helloworld"