Skip to content

String extensions

Amethyst’s String is .NET’s System.String, and every .NET member is available under its snake_case name:

title = " Hello, World ".trim
puts title.to_upper # HELLO, WORLD
puts title.starts_with("Hello") # True
puts title.length # 12

Amethyst.Core adds the conveniences below as .NET extension methods. Indexing and slicing strings with [] is listed under Enumerable[T].

Receiver Signature Description
Regex matches?(input: String): Bool Whether the pattern matches anywhere in input.
Regex match(input: String): Match? The first match, or nil. Takes precedence over .NET’s Regex.Match.
String match(regex: Regex): Match? The first match of regex, or nil.
String match_all(regex: Regex): Array[Match] Every match of regex.
String sub(regex: Regex, replacement: String): String Replaces the first match.
String sub(regex: Regex, &block: (String) -> String): String Replaces the first match with the block’s result.
String gsub(regex: Regex, replacement: String): String Replaces every match.
String gsub(regex: Regex, &block: (String) -> String): String Replaces every match with the block’s result.
String split(regex: Regex): Array[String] Splits around every match.
Match [](index: Int): String? A capture group by number, or nil if it didn’t take part in the match.
Match [](name: Symbol): String? A named capture group, or nil if it didn’t take part in the match.
Match start: Int The index where the match starts.
Match end: Int The index just past the match.
Receiver Signature Description
String to_int: Int, to_i: Int Parses an integer; raises if the string isn’t one.
String to_float: Float, to_f: Float Parses a number with the invariant culture.
String to_sym: Symbol The symbol with this name.
Int, Long, Float, Single, Decimal to_int: Int Converts to Int, truncating a fraction; a Long that doesn’t fit wraps around.
Int, Long, Float to_long: Long Converts to Long, truncating a fraction.
Int, Long, Float, Single, Decimal to_float: Float Converts to Float.
Int, Long, Float to_byte: Byte Converts to Byte, keeping the low 8 bits.
Receiver Signature Description
any flags enum includes?(flag: T): Bool Whether the value has flag set.
any Disposable use(&block: (T) -> Void): Void Passes the object to the block and disposes of it however the block exits.
any Disposable use[R](&block: (T) -> R): R Like use, returning the block’s result.