Skip to content

class String
inherits Reference

A String represents an immutable sequence of UTF-8 characters.

A String is typically created with a string literal, enclosing UTF-8 characters in double quotes:

"hello world"

A backslash can be used to denote some characters inside the string:

"\"" # double quote
"\\" # backslash
"\e" # escape
"\f" # form feed
"\n" # newline
"\r" # carriage return
"\t" # tab
"\v" # vertical tab

You can use a backslash followed by an u and four hexadecimal characters to denote a unicode codepoint written:

"\u0041" # == "A"

Or you can use curly braces and specify up to six hexadecimal numbers (0 to 10FFFF):

"\u{41}" # == "A"

A string can span multiple lines:

"hello
      world" # same as "hello\n      world"

Note that in the above example trailing and leading spaces, as well as newlines, end up in the resulting string. To avoid this, you can split a string into multiple lines by joining multiple literals with a backslash:

"hello " \
"world, " \
"no newlines" # same as "hello world, no newlines"

Alternatively, a backslash followed by a newline can be inserted inside the string literal:

"hello \
     world, \
     no newlines" # same as "hello world, no newlines"

In this case, leading whitespace is not included in the resulting string.

If you need to write a string that has many double quotes, parentheses, or similar characters, you can use alternative literals:

# Supports double quotes and nested parentheses
%(hello ("world")) # same as "hello (\"world\")"

# Supports double quotes and nested brackets
%[hello ["world"]] # same as "hello [\"world\"]"

# Supports double quotes and nested curlies
%{hello {"world"}} # same as "hello {\"world\"}"

# Supports double quotes and nested angles
%<hello <"world">> # same as "hello <\"world\">"

To create a String with embedded expressions, you can use string interpolation:

a = 1
b = 2
"sum = #{a + b}" # "sum = 3"

This ends up invoking Object#to_s(IO) on each expression enclosed by #{...}.

If you need to dynamically build a string, use String#build or IO::Memory.

Non UTF-8 valid strings

String might end up being conformed of bytes which are an invalid byte sequence according to UTF-8. This can happen if the string is created via one of the constructors that accept bytes, or when getting a string from String.build or IO::Memory. No exception will be raised, but invalid byte sequences, when asked as chars, will use the unicode replacement char (value 0xFFFD). For example:

# here 255 is not a valid byte value in the UTF-8 encoding
string = String.new(Bytes[255, 97])
string.valid_encoding? # => false

# The first char here is the unicode replacement char
string.chars # => ['�', 'a']

One can also create strings with specific byte value in them by using octal and hexadecimal escape sequences:

# Octal escape sequences
"\101" # # => "A"
"\12"  # # => "\n"
"\1"   # string with one character with code point 1
"\377" # string with one byte with value 255

# Hexadecimal escape sequences
"\x41" # # => "A"
"\xFF" # string with one byte with value 255

The reason for allowing strings that don't have a valid UTF-8 sequence is that the world is full of content that isn't properly encoded, and having a program raise an exception or stop because of this is not good. It's better if programs are more resilient, but show a replacement character when there's an error in incoming data.

Included modules

Comparable

Class methods

.from_json_object_key?(key : String)

View source

.from_utf16(pointer : Pointer(UInt16)) : Tuple(String, Pointer(UInt16))

Decodes the given slice UTF-16 sequence into a String and returns the pointer after reading. The string ends when a zero value is found.

slice = Slice[104_u16, 105_u16, 0_u16, 55296_u16, 56485_u16, 0_u16]
String.from_utf16(slice) # => "hi\0000𐂥"
pointer = slice.to_unsafe
string, pointer = String.from_utf16(pointer) # => "hi"
string, pointer = String.from_utf16(pointer) # => "𐂥"

Invalid values are encoded using the unicode replacement char with codepoint 0xfffd.

View source

.interpolation(*values : *T) forall T

Implementation of string interpolation of multiple, possibly non-string values.

For example, this code will end up invoking this method:

value1 = "hello"
value2 = 123
"#{value1} #{value2}!" # same as String.interpolation(value1, " ", value2, "!")

In this case the implementation will call String.build with the given values.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.interpolation(*values : String)

Implementation of string interpolation of multiple string values.

For example, this code will end up invoking this method:

value1 = "hello"
value2 = "world"
"#{value1} #{value2}!" # same as String.interpolation(value1, " ", value2, "!")

In this case the implementation can pre-compute the needed string bytesize and so it's a bit more performant than interpolating non-string values.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.interpolation(value)

Implementation of string interpolation of a single non-string value.

For example, this code will end up invoking this method:

value = 123
"#{value}" # same as String.interpolation(value)

In this case the implementation just returns the result of calling value.to_s.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.interpolation(value : String)

Implementation of string interpolation of a single string.

For example, this code will end up invoking this method:

value = "hello"
"#{value}" # same as String.interpolation(value)

In this case the implementation just returns the same string.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.interpolation(char : Char, value : String)

Implementation of string interpolation of a char and a string.

For example, this code will end up invoking this method:

char = '!'
"#{char}hello" # same as String.interpolation(char, "hello")

In this case the implementation just does char + value.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.interpolation(value : String, char : Char)

Implementation of string interpolation of a string and a char.

For example, this code will end up invoking this method:

char = '!'
"hello#{char}" # same as String.interpolation("hello", char)

In this case the implementation just does value + char.

Note

there should never be a need to call this method instead of using string interpolation.

View source

.build(capacity = 64, &) : self

Builds a String by creating a String::Builder with the given initial capacity, yielding it to the block and finally getting a String out of it. The String::Builder automatically resizes as needed.

str = String.build do |str|
  str << "hello "
  str << 1
end
str # => "hello 1"
View source

.from_utf16(slice : Slice(UInt16)) : String

Decodes the given slice UTF-16 sequence into a String.

Invalid values are encoded using the unicode replacement char with codepoint 0xfffd.

slice = Slice[104_u16, 105_u16, 32_u16, 55296_u16, 56485_u16]
String.from_utf16(slice) # => "hi 𐂥"
View source

.new(capacity : Int

Creates a new String by allocating a buffer (Pointer(UInt8)) with the given capacity, then yielding that buffer. The block must return a tuple with the bytesize and size (UTF-8 codepoints count) of the String. If the returned size is zero, the UTF-8 codepoints count will be lazily computed.

The bytesize returned by the block must be less than or equal to the capacity given to this String, otherwise ArgumentError is raised.

If you need to build a String where the maximum capacity is unknown, use String#build.

str = String.new(4) do |buffer|
  buffer[0] = 'a'.ord.to_u8
  buffer[1] = 'b'.ord.to_u8
  {2, 2}
end
str # => "ab"
View source

.new(slice : Bytes)

Creates a String from the given slice. Bytes will be copied from the slice.

This method is always safe to call, and the resulting string will have the contents and size of the slice.

slice = Slice.new(4) { |i| ('a'.ord + i).to_u8 }
String.new(slice) # => "abcd"
View source

.new(chars : Pointer(UInt8))

Creates a String from a pointer. Bytes will be copied from the pointer.

This method is unsafe: the pointer must point to data that eventually contains a zero byte that indicates the ends of the string. Otherwise, the result of this method is undefined and might cause a segmentation fault.

This method is typically used in C bindings, where you get a char* from a library and the library guarantees that this pointer eventually has an ending zero byte.

ptr = Pointer.malloc(5) { |i| i == 4 ? 0_u8 : ('a'.ord + i).to_u8 }
String.new(ptr) # => "abcd"
View source

.new(chars : Pointer(UInt8), bytesize, size = 0)

Creates a new String from a pointer, indicating its bytesize count and, optionally, the UTF-8 codepoints count (size). Bytes will be copied from the pointer.

If the given size is zero, the amount of UTF-8 codepoints will be lazily computed when needed.

ptr = Pointer.malloc(4) { |i| ('a'.ord + i).to_u8 }
String.new(ptr, 2) # => "ab"
View source

.new(bytes : Bytes, encoding : String, invalid : Symbol? = nil) : String

Creates a new String from the given bytes, which are encoded in the given encoding.

The invalid argument can be: * nil: an exception is raised on invalid byte sequences * :skip: invalid byte sequences are ignored

slice = Slice.new(2, 0_u8)
slice[0] = 186_u8
slice[1] = 195_u8
String.new(slice, "GB2312") # => "好"
View source

Methods

#%(other)

Interpolates other into the string using top-level ::sprintf.

"I have %d apples" % 5                                             # => "I have 5 apples"
"%s, %s, %s, D" % ['A', 'B', 'C']                                  # => "A, B, C, D"
"sum: %{one} + %{two} = %{three}" % {one: 1, two: 2, three: 1 + 2} # => "sum: 1 + 2 = 3"
"I have %<apples>s apples" % {apples: 4}                           # => "I have 4 apples"
View source

#*(times : Int)

Makes a new String by adding str to itself times times.

"Developers! " * 4
# => "Developers! Developers! Developers! Developers! "
View source

#+(char : Char)

Concatenates str and other.

"abc" + "def" # => "abcdef"
"abc" + 'd'   # => "abcd"
View source

#+(other : self)

Concatenates str and other.

"abc" + "def" # => "abcdef"
"abc" + 'd'   # => "abcd"
View source

#<=>(other : self)

The comparison operator.

Compares this string with other, returning -1, 0 or 1 depending on whether this string is less, equal or greater than other.

Comparison is done byte-per-byte: if a byte is less then the other corresponding byte, -1 is returned and so on.

If the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered greater than the shorter one.

"abcdef" <=> "abcde"   # => 1
"abcdef" <=> "abcdef"  # => 0
"abcdef" <=> "abcdefg" # => -1
"abcdef" <=> "ABCDEF"  # => 1
View source

#==(other : self) : Bool

Returns true if this string is equal to *other*. Comparison is done byte-per-byte: if a byte is different from the corresponding byte,false` is returned and so on.

See #compare for more comparison options.

View source

#=~(regex : Regex)

Tests whether str matches regex. If successful, it returns the position of the first match. If unsuccessful, it returns nil.

If the argument isn't a Regex, it returns nil.

"Haystack" =~ /ay/ # => 1
"Haystack" =~ /z/  # => nil

"Haystack" =~ 45 # => nil
View source

#=~(other)

Tests whether str matches regex. If successful, it returns the position of the first match. If unsuccessful, it returns nil.

If the argument isn't a Regex, it returns nil.

"Haystack" =~ /ay/ # => 1
"Haystack" =~ /z/  # => nil

"Haystack" =~ 45 # => nil
View source

#[](regex : Regex, group)

View source

#[](start : Int, count : Int)

Returns a substring starting from the start character of size count.

start can can be negative to start counting from the end of the string.

Raises IndexError if the start index is out of bounds.

Raises ArgumentError if count is negative.

View source

#[](range : Range)

Returns a substring by using a Range's begin and end as character indices. Indices can be negative to start counting from the end of the string.

Raises IndexError if the range's start is out of bounds.

"hello"[0..2]   # => "hel"
"hello"[0...2]  # => "he"
"hello"[1..-1]  # => "ello"
"hello"[1...-1] # => "ell"
"hello"[6..7]   # raises IndexError
View source

#[](regex : Regex)

View source

#[](str : String | Char)

View source

#[](index : Int)

Returns the Char at the given index.

Negative indices can be used to start counting from the end of the string.

Raises IndexError if the index is out of bounds.

"hello"[0]  # => 'h'
"hello"[1]  # => 'e'
"hello"[-1] # => 'o'
"hello"[-2] # => 'l'
"hello"[5]  # raises IndexError
View source

#[]?(start : Int, count : Int)

Like #[Int, Int] but returns nil if the start index is out of bounds.

View source

#[]?(regex : Regex, group)

View source

#[]?(range : Range)

Like #[Range], but returns nil if the range's start is out of bounds.

"hello"[6..7]? # => nil
"hello"[6..]?  # => nil
View source

#[]?(index : Int)

View source

#[]?(str : String | Char)

View source

#[]?(regex : Regex)

View source

#ascii_only?

Returns true if this String is comprised in its entirety by ASCII characters.

"hello".ascii_only? # => true
"你好".ascii_only?    # => false
View source

#blank?

Returns true if this string consists exclusively of unicode whitespace.

"".blank?        # => true
"   ".blank?     # => true
"   a   ".blank? # => false
View source

#byte_at(index) : UInt8

Returns the byte at the given index.

Raises IndexError if the index is out of bounds.

"¥hello".byte_at(0)  # => 194
"¥hello".byte_at(1)  # => 165
"¥hello".byte_at(2)  # => 104
"¥hello".byte_at(-1) # => 111
"¥hello".byte_at(6)  # => 111
"¥hello".byte_at(7)  # raises IndexError
View source

#byte_at

Returns the byte at the given index, or yields if out of bounds.

"¥hello".byte_at(6) { "OUT OF BOUNDS" } # => 111
"¥hello".byte_at(7) { "OUT OF BOUNDS" } # => "OUT OF BOUNDS"
View source

#byte_at?(index) : UInt8?

Returns the byte at the given index, or nil if out of bounds.

"¥hello".byte_at?(0)  # => 194
"¥hello".byte_at?(1)  # => 165
"¥hello".byte_at?(2)  # => 104
"¥hello".byte_at?(-1) # => 111
"¥hello".byte_at?(6)  # => 111
"¥hello".byte_at?(7)  # => nil
View source

#byte_index(search : String, offset = 0) : Int32?

Returns the byte index of search in the string, or nil if the string is not present. If offset is present, it defines the position to start the search.

Negative offset can be used to start the search from the end of the string.

"¥hello".byte_index("hello")              # => 2
"hello".byte_index("world")               # => nil
"Dizzy Miss Lizzy".byte_index("izzy")     # => 1
"Dizzy Miss Lizzy".byte_index("izzy", 2)  # => 12
"Dizzy Miss Lizzy".byte_index("izzy", -4) # => 12
"Dizzy Miss Lizzy".byte_index("izzy", -3) # => nil
View source

#byte_index(byte : Int, offset = 0) : Int32?

Returns the index of the first occurrence of byte in the string, or nil if not present. If offset is present, it defines the position to start the search.

Negative offset can be used to start the search from the end of the string.

"Hello, World".byte_index(0x6f)             # => 4
"Hello, World".byte_index(0x5a)             # => nil
"Hello, World".byte_index(0x6f, 5)          # => 8
"💣".byte_index(0xA3)                        # => 3
"Dizzy Miss Lizzy".byte_index('z'.ord)      # => 2
"Dizzy Miss Lizzy".byte_index('z'.ord, 3)   # => 3
"Dizzy Miss Lizzy".byte_index('z'.ord, -4)  # => 13
"Dizzy Miss Lizzy".byte_index('z'.ord, -17) # => nil
View source

#byte_index_to_char_index(index)

Returns the char index of a byte index, or nil if out of bounds.

It is valid to pass #bytesize to index, and in this case the answer will be the size of this string.

View source

#byte_slice(start : Int) : String

Returns a substring starting from the start byte.

start can can be negative to start counting from the end of the string.

This method should be avoided, unless the string is proven to be ASCII-only (for example #ascii_only?), or the byte positions are known to be at character boundaries. Otherwise, multi-byte characters may be split, leading to an invalid UTF-8 encoding.

Raises IndexError if start index is out of bounds.

"hello".byte_slice(0)  # => "hello"
"hello".byte_slice(2)  # => "llo"
"hello".byte_slice(-2) # => "lo"
"¥hello".byte_slice(2) # => "hello"
"¥hello".byte_slice(1) # => "�hello" (invalid UTF-8 character)
"hello".byte_slice(6)  # raises IndexError
"hello".byte_slice(-6) # raises IndexError
View source

#byte_slice(start : Int, count : Int) : String

Returns a new string built from count bytes starting at start byte.

start can can be negative to start counting from the end of the string. If count is bigger than the number of bytes from start to #bytesize, only remaining bytes are returned.

This method should be avoided, unless the string is proven to be ASCII-only (for example #ascii_only?), or the byte positions are known to be at character boundaries. Otherwise, multi-byte characters may be split, leading to an invalid UTF-8 encoding.

Raises IndexError if the start index is out of bounds.

Raises ArgumentError if count is negative.

"hello".byte_slice(0, 2)   # => "he"
"hello".byte_slice(0, 100) # => "hello"
"hello".byte_slice(-2, 3)  # => "he"
"hello".byte_slice(-2, 5)  # => "he"
"hello".byte_slice(-2, 5)  # => "he"
"¥hello".byte_slice(0, 2)  # => "¥"
"¥hello".byte_slice(2, 2)  # => "he"
"¥hello".byte_slice(0, 1)  # => "�" (invalid UTF-8 character)
"¥hello".byte_slice(1, 1)  # => "�" (invalid UTF-8 character)
"¥hello".byte_slice(1, 2)  # => "�h" (invalid UTF-8 character)
"hello".byte_slice(6, 2)   # raises IndexError
"hello".byte_slice(-6, 2)  # raises IndexError
"hello".byte_slice(0, -2)  # raises ArgumentError
View source

#byte_slice?(start : Int, count : Int) : String?

Like byte_slice(Int, Int) but returns Nil if the start index is out of bounds.

Raises ArgumentError if count is negative.

"hello".byte_slice?(0, 2)   # => "he"
"hello".byte_slice?(0, 100) # => "hello"
"hello".byte_slice?(6, 2)   # => nil
"hello".byte_slice?(-6, 2)  # => nil
"hello".byte_slice?(0, -2)  # raises ArgumentError
View source

#bytes

Returns this string's bytes as an Array(UInt8).

"hello".bytes # => [104, 101, 108, 108, 111]
"你好".bytes    # => [228, 189, 160, 229, 165, 189]
View source

#bytesize : Int32

Returns the number of bytes in this string.

"hello".bytesize # => 5
"你好".bytesize    # => 6
View source

#camelcase(io : IO, options : Unicode::CaseOptions = Unicode::CaseOptions::None, *, lower : Bool = false) : Nil

Writes an camelcased version of self to the given io.

If lower is true, lower camelcase will be written (the first letter is downcased).

io = IO::Memory.new
"eiffel_tower".camelcase io
io.to_s # => "EiffelTower"
View source

#camelcase(options : Unicode::CaseOptions = Unicode::CaseOptions::None, *, lower : Bool = false) : String

Converts underscores to camelcase boundaries.

If lower is true, lower camelcase will be returned (the first letter is downcased).

"eiffel_tower".camelcase                                            # => "EiffelTower"
"empire_state_building".camelcase(lower: true)                      # => "empireStateBuilding"
"isolated_integer".camelcase(options: Unicode::CaseOptions::Turkic) # => "İsolatedİnteger"
View source

#capitalize(options : Unicode::CaseOptions = :none) : String

Returns a new String with the first letter converted to uppercase and every subsequent letter converted to lowercase.

"hEllO".capitalize # => "Hello"
View source

#capitalize(io : IO, options : Unicode::CaseOptions = :none) : Nil

Writes a capitalized version of self to the given io.

io = IO::Memory.new
"hEllO".capitalize io
io.to_s # => "Hello"
View source

#center(len : Int, char : Char = ' ')

Adds instances of char to left and right of the string until it is at least size of len.

"Purple".center(8)      # => " Purple "
"Purple".center(8, '-') # => "-Purple-"
"Purple".center(9, '-') # => "-Purple--"
"Aubergine".center(8)   # => "Aubergine"
View source

#center(io : IO, len : Int, char : Char = ' ') : Nil

Adds instances of char to left and right of the string until it is at least size of len, then appends the result to the given IO.

io = IO::Memory.new
"Purple".center(io, 9, '-')
io.to_s # => "-Purple--"
View source

#char_at(index : Int

Returns the Char at the given index, or result of running the given block if out of bounds.

Negative indices can be used to start counting from the end of the string.

"hello".char_at(4) { 'x' }  # => 'o'
"hello".char_at(5) { 'x' }  # => 'x'
"hello".char_at(-1) { 'x' } # => 'o'
"hello".char_at(-5) { 'x' } # => 'h'
"hello".char_at(-6) { 'x' } # => 'x'
View source

#char_at(index : Int) : Char

Returns the Char at the given index.

Negative indices can be used to start counting from the end of the string.

Raises IndexError if the index is out of bounds.

"hello".char_at(0)  # => 'h'
"hello".char_at(1)  # => 'e'
"hello".char_at(-1) # => 'o'
"hello".char_at(-2) # => 'l'
"hello".char_at(5)  # raises IndexError
View source

#char_index_to_byte_index(index)

Returns the byte index of a char index, or nil if out of bounds.

It is valid to pass #size to index, and in this case the answer will be the bytesize of this string.

"hello".char_index_to_byte_index(1) # => 1
"hello".char_index_to_byte_index(5) # => 5
"こんにちは".char_index_to_byte_index(1) # => 3
"こんにちは".char_index_to_byte_index(5) # => 15
View source

#chars

Returns an Array of all characters in the string.

"ab☃".chars # => ['a', 'b', '☃']
View source

#check_no_null_byte(name = nil)

Raises an ArgumentError if self has null bytes. Returns self otherwise.

This method should sometimes be called before passing a String to a C function.

View source

#chomp(suffix : Char)

Returns a new String with suffix removed from the end of the string. If suffix is '\n' then "\r\n" is also removed if the string ends with it.

"hello".chomp('o') # => "hell"
"hello".chomp('a') # => "hello"
View source

#chomp(suffix : String)

Returns a new String with suffix removed from the end of the string. If suffix is "\n" then "\r\n" is also removed if the string ends with it.

"hello".chomp("llo") # => "he"
"hello".chomp("ol")  # => "hello"
View source

#chomp

Returns a new String with the last carriage return removed (that is, it will remove \n, \r, and \r\n).

"string\r\n".chomp # => "string"
"string\n\r".chomp # => "string\n"
"string\n".chomp   # => "string"
"string".chomp     # => "string"
"x".chomp.chomp    # => "x"
View source

#clone : String

Returns self.

View source

#codepoint_at(index) : Int32

Returns the codepoint of the character at the given index.

Negative indices can be used to start counting from the end of the string.

Raises IndexError if the index is out of bounds.

See also: Char#ord.

"hello".codepoint_at(0)  # => 104
"hello".codepoint_at(-1) # => 111
"hello".codepoint_at(5)  # raises IndexError
View source

#codepoints

Returns an Array of the codepoints that make the string.

"ab☃".codepoints # => [97, 98, 9731]

See also: Char#ord.

View source

#compare(other : String, case_insensitive = false, options = Unicode::CaseOptions::None)

Compares this string with other, returning -1, 0 or 1 depending on whether this string is less, equal or greater than other, optionally in a case_insensitive manner.

"abcdef".compare("abcde")   # => 1
"abcdef".compare("abcdef")  # => 0
"abcdef".compare("abcdefg") # => -1
"abcdef".compare("ABCDEF")  # => 1

"abcdef".compare("ABCDEF", case_insensitive: true) # => 0
"abcdef".compare("ABCDEG", case_insensitive: true) # => -1

"heIIo".compare("heııo", case_insensitive: true, options: Unicode::CaseOptions::Turkic) # => 0
View source

#count(other : Char)

Counts the occurrences of other char in this string.

"aabbcc".count('a') # => 2
View source

#count

Yields each char in this string to the block, returns the number of times the block returned a truthy value.

"aabbcc".count &.in?('a', 'b') # => 4
View source

#count(*sets)

Sets should be a list of strings following the rules described at Char#in_set?. Returns the number of characters in this string that match the given set.

View source

#delete

Yields each char in this string to the block. Returns a new String with all characters for which the block returned a truthy value removed.

"aabbcc".delete &.in?('a', 'b') # => "cc"
View source

#delete(char : Char)

Returns a new String with all occurrences of char removed.

"aabbcc".delete('b') # => "aacc"
View source

#delete(*sets)

Sets should be a list of strings following the rules described at Char#in_set?. Returns a new String with all characters that match the given set removed.

"aabbccdd".delete("a-c") # => "dd"
View source

#delete_at(index : Int) : String

Returns a new string that results from deleting the character at the given index.

"abcde".delete_at(0) # => "bcde"
"abcde".delete_at(2) # => "abde"
"abcde".delete_at(4) # => "abcd"

A negative index counts from the end of the string:

"abcde".delete_at(-2) # => "abce"

If index is outside the bounds of the string, IndexError is raised.

View source

#delete_at(range : Range)

Returns a new string that results from deleting characters at the given range.

"abcdef".delete_at(1..3) # => "aef"

Negative indices can be used to start counting from the end of the string:

"abcdef".delete_at(-3..-2) # => "abcf"

Raises IndexError if any index is outside the bounds of this string.

View source

#delete_at(index : Int, count : Int) : String

Returns a new string that results from deleting count characters starting at index.

"abcdefg".delete_at(1, 3) # => "aefg"

Deleting more characters than those in the string is valid, and just results in deleting up to the last character:

"abcdefg".delete_at(3, 10) # => "abc"

A negative index counts from the end of the string:

"abcdefg".delete_at(-3, 2) # => "abcdg"

If count is negative, ArgumentError is raised.

If index is outside the bounds of the string, ArgumentError is raised.

However, index can be the position that is exactly the end of the string:

"abcd".delete_at(4, 3) # => "abcd"
View source

#downcase(options : Unicode::CaseOptions = :none) : String

Returns a new String with each uppercase letter replaced with its lowercase counterpart.

"hEllO".downcase # => "hello"
View source

#downcase(io : IO, options : Unicode::CaseOptions = :none) : Nil

Writes a downcased version of self to the given io.

io = IO::Memory.new
"hEllO".downcase io
io.to_s # => "hello"
View source

#dump(io : IO) : Nil

Appends self to the given IO object using character escapes for special characters and non-ascii characters (unicode codepoints > 128), wrapped in quotes.

View source

#dump : String

Returns a representation of self using character escapes for special characters and non-ascii characters (unicode codepoints > 128), wrapped in quotes.

"\u{1f48e} - à la carte\n".dump # => %("\\u{1F48E} - \\u00E0 la carte\\n")
View source

#dump_unquoted(io : IO) : Nil

Appends self to the given IO object using character escapes for special characters and non-ascii characters (unicode codepoints > 128), but not wrapped in quotes.

View source

#dump_unquoted : String

Returns a representation of self using character escapes for special characters and non-ascii characters (unicode codepoints > 128), but not wrapped in quotes.

"\u{1f48e} - à la carte\n".dump_unquoted # => %(\\u{1F48E} - \\u00E0 la carte\\n)
View source

#dup : String

Returns self.

View source

#each_byte

Returns an Iterator over each byte in the string.

bytes = "ab☃".each_byte
bytes.next # => 97
bytes.next # => 98
bytes.next # => 226
bytes.next # => 152
bytes.next # => 131
View source

#each_byte

Yields each byte in the string to the block.

array = [] of UInt8
"ab☃".each_byte do |byte|
  array << byte
end
array # => [97, 98, 226, 152, 131]
View source

#each_char

Returns an Iterator over each character in the string.

chars = "ab☃".each_char
chars.next # => 'a'
chars.next # => 'b'
chars.next # => '☃'
View source

#each_char(&) : Nil

Yields each character in the string to the block.

array = [] of Char
"ab☃".each_char do |char|
  array << char
end
array # => ['a', 'b', '☃']
View source

#each_char_with_index(offset = 0

Yields each character and its index in the string to the block.

array = [] of Tuple(Char, Int32)
"ab☃".each_char_with_index do |char, index|
  array << {char, index}
end
array # => [{'a', 0}, {'b', 1}, {'☃', 2}]

Accepts an optional offset parameter, which tells it to start counting from there.

View source

#each_codepoint

Returns an Iterator for each codepoint.

codepoints = "ab☃".each_codepoint
codepoints.next # => 97
codepoints.next # => 98
codepoints.next # => 9731

See also: Char#ord.

View source

#each_codepoint

Yields each codepoint to the block.

array = [] of Int32
"ab☃".each_codepoint do |codepoint|
  array << codepoint
end
array # => [97, 98, 9731]

See also: Char#ord.

View source

#each_line(chomp = true)

Returns an Iterator which yields each line of this string (see String#each_line).

View source

#each_line(chomp = true, &block : String -> ) : Nil

Splits the string after each newline and yields each line to a block.

haiku = "the first cold shower
even the monkey seems to want
a little coat of straw"
haiku.each_line do |stanza|
  puts stanza
end
# output:
# the first cold shower
# even the monkey seems to want
# a little coat of straw
View source

#empty?

Returns true if this is the empty string, "".

View source

#encode(encoding : String, invalid : Symbol? = nil) : Bytes

Returns a slice of bytes containing this string encoded in the given encoding.

The invalid argument can be: * nil: an exception is raised on invalid byte sequences * :skip: invalid byte sequences are ignored

"好".encode("GB2312") # => Bytes[186, 195]
"好".bytes            # => [229, 165, 189]
View source

#ends_with?(re : Regex) : Bool

Returns true if the regular expression re matches at the end of this string.

"22hello".ends_with?(/[0-9]/) # => false
"22hello".ends_with?(/[a-z]/) # => true
"22h".ends_with?(/[a-z]/)     # => true
"22h".ends_with?(/[A-Z]/)     # => false
"22h".ends_with?(/[a-z]{2}/)  # => false
"22hh".ends_with?(/[a-z]{2}/) # => true
View source

#ends_with?(char : Char) : Bool

Returns true if this string ends with the given char.

"hello".ends_with?('o') # => true
"hello".ends_with?('l') # => false
View source

#ends_with?(str : String) : Bool

Returns true if this string ends with the given str.

"hello".ends_with?("o")  # => true
"hello".ends_with?("lo") # => true
"hello".ends_with?("ll") # => false
View source

#gsub(tuple : NamedTuple)

Returns a String where all chars in the given named tuple are replaced by the corresponding tuple values.

"hello".gsub({e: 'a', l: 'd'}) # => "haddo"
View source

#gsub(hash : Hash(Char, _))

Returns a String where all chars in the given hash are replaced by the corresponding hash values.

"hello".gsub({'e' => 'a', 'l' => 'd'}) # => "haddo"
View source

#gsub(string : String, replacement)

Returns a String where all occurrences of the given string are replaced with the given replacement.

"hello yellow".gsub("ll", "dd") # => "heddo yeddow"
View source

#gsub(pattern : Regex, replacement, backreferences = true)

Returns a String where all occurrences of the given pattern are replaced with the given replacement.

"hello".gsub(/[aeiou]/, '*') # => "h*ll*"

Within replacement, the special match variable $~ will not refer to the current match.

If backreferences is true (the default value), replacement can include backreferences:

"hello".gsub(/[aeiou]/, "(\\0)") # => "h(e)ll(o)"

When substitution is performed, any backreferences found in replacement will be replaced with the contents of the corresponding capture group in pattern. Backreferences to capture groups that were not present in pattern or that did not match will be skipped. See Regex for information about capture groups.

Backreferences are expressed in the form "\\d", where d is a group number, or "\\k<name>" where name is the name of a named capture group. A sequence of literal characters resembling a backreference can be expressed by placing "\\" before the sequence.

"foo".gsub(/o/, "x\\0x")                  # => "fxoxxox"
"foofoo".gsub(/(?<bar>oo)/, "|\\k<bar>|") # => "f|oo|f|oo|"
"foo".gsub(/o/, "\\\\0")                  # => "f\\0\\0"

Raises ArgumentError if an incomplete named back-reference is present in replacement.

Raises IndexError if a named group referenced in replacement is not present in pattern.

View source

#gsub(pattern : Regex, hash : Hash(String, _) | NamedTuple)

Returns a String where all occurrences of the given pattern are replaced with a hash of replacements. If the hash contains the matched pattern, the corresponding value is used as a replacement. Otherwise the match is not included in the returned string.

# "he" and "l" are matched and replaced,
# but "o" is not and so is not included
"hello".gsub(/(he|l|o)/, {"he": "ha", "l": "la"}) # => "halala"
View source

#gsub(char : Char, replacement)

Returns a String where all occurrences of the given char are replaced with the given replacement.

"hello".gsub('l', "lo")      # => "heloloo"
"hello world".gsub('o', 'a') # => "hella warld"
View source

#gsub(&block : Char -> _)

Returns a String where each character yielded to the given block is replaced by the block's return value.

"hello".gsub { |char| char + 1 } # => "ifmmp"
"hello".gsub { "hi" }            # => "hihihihihi"
View source

#gsub(string : String

Returns a String where all occurrences of the given string are replaced with the block's value.

"hello yellow".gsub("ll") { "dd" } # => "heddo yeddow"
View source

#gsub(pattern : Regex

Returns a String where all occurrences of the given pattern are replaced by the block value's value.

"hello".gsub(/./) { |s| s[0].ord.to_s + ' ' } # => "104 101 108 108 111 "
View source

#has_back_references?

This returns true if this string has '\\' in it. It might not be a back reference, but '\\' is probably used for back references, so this check is faster than parsing the whole thing.

View source

#hash(hasher)

View source

#hexbytes : Bytes

Interprets this string as containing a sequence of hexadecimal values and decodes it as a slice of bytes. Two consecutive bytes in the string represent a byte in the returned slice.

Raises ArgumentError if this string does not denote an hexstring.

"0102031aff".hexbytes  # => Bytes[1, 2, 3, 26, 255]
"1".hexbytes           # raises ArgumentError
"hello world".hexbytes # raises ArgumentError
View source

#hexbytes? : Bytes?

Interprets this string as containing a sequence of hexadecimal values and decodes it as a slice of bytes. Two consecutive bytes in the string represent a byte in the returned slice.

Returns nil if this string does not denote an hexstring.

"0102031aff".hexbytes?  # => Bytes[1, 2, 3, 26, 255]
"1".hexbytes?           # => nil
"hello world".hexbytes? # => nil
View source

#includes?(search : Char | String)

Returns true if the string contains search.

"Team".includes?('i')            # => false
"Dysfunctional".includes?("fun") # => true
View source

#index(search : Regex, offset = 0)

Returns the index of the first occurrence of search in the string, or nil if not present. If offset is present, it defines the position to start the search.

"Hello, World".index('o')    # => 4
"Hello, World".index('Z')    # => nil
"Hello, World".index("o", 5) # => 8
"Hello, World".index("H", 2) # => nil
"Hello, World".index(/[ ]+/) # => 6
"Hello, World".index(/\d+/)  # => nil
View source

#index(search : String, offset = 0)

Returns the index of the first occurrence of search in the string, or nil if not present. If offset is present, it defines the position to start the search.

"Hello, World".index('o')    # => 4
"Hello, World".index('Z')    # => nil
"Hello, World".index("o", 5) # => 8
"Hello, World".index("H", 2) # => nil
"Hello, World".index(/[ ]+/) # => 6
"Hello, World".index(/\d+/)  # => nil
View source

#index(search : Char, offset = 0)

Returns the index of the first occurrence of search in the string, or nil if not present. If offset is present, it defines the position to start the search.

"Hello, World".index('o')    # => 4
"Hello, World".index('Z')    # => nil
"Hello, World".index("o", 5) # => 8
"Hello, World".index("H", 2) # => nil
"Hello, World".index(/[ ]+/) # => 6
"Hello, World".index(/\d+/)  # => nil
View source

#insert(index : Int, other : Char)

Returns a new String that results of inserting other in self at index. Negative indices count from the end of the string, and insert after the given index.

Raises IndexError if the index is out of bounds.

"abcd".insert(0, 'X')  # => "Xabcd"
"abcd".insert(3, 'X')  # => "abcXd"
"abcd".insert(4, 'X')  # => "abcdX"
"abcd".insert(-3, 'X') # => "abXcd"
"abcd".insert(-1, 'X') # => "abcdX"

"abcd".insert(5, 'X')  # raises IndexError
"abcd".insert(-6, 'X') # raises IndexError
View source

#insert(index : Int, other : String)

Returns a new String that results of inserting other in self at index. Negative indices count from the end of the string, and insert after the given index.

Raises IndexError if the index is out of bounds.

"abcd".insert(0, "FOO")  # => "FOOabcd"
"abcd".insert(3, "FOO")  # => "abcFOOd"
"abcd".insert(4, "FOO")  # => "abcdFOO"
"abcd".insert(-3, "FOO") # => "abFOOcd"
"abcd".insert(-1, "FOO") # => "abcdFOO"

"abcd".insert(5, "FOO")  # raises IndexError
"abcd".insert(-6, "FOO") # raises IndexError
View source

#inspect : String

Returns a representation of self using character escapes for special characters and wrapped in quotes.

"\u{1f48e} - à la carte\n".inspect # => %("\u{1F48E} - à la carte\\n")
View source

#inspect(io : IO) : Nil

Appends self to the given IO object using character escapes for special characters and wrapped in double quotes.

View source

#inspect_unquoted : String

Returns a representation of self using character escapes for special characters but not wrapped in quotes.

"\u{1f48e} - à la carte\n".inspect_unquoted # => %(\u{1F48E} - à la carte\\n)
View source

#inspect_unquoted(io : IO) : Nil

Appends self to the given IO object using character escapes for special characters but not wrapped in quotes.

View source

#lchop : String

Returns a new String with the first char removed from it. Applying lchop to an empty string returns an empty string.

"hello".lchop # => "ello"
"".lchop      # => ""
View source

#lchop(prefix : Char | String) : String

Returns a new String with prefix removed from the beginning of the string.

"hello".lchop('h')   # => "ello"
"hello".lchop('g')   # => "hello"
"hello".lchop("hel") # => "lo"
"hello".lchop("eh")  # => "hello"
View source

#lchop?(prefix : Char | String) : String?

Returns a new String with prefix removed from the beginning of the string if possible, else returns nil.

"hello".lchop?('h')   # => "ello"
"hello".lchop?('g')   # => nil
"hello".lchop?("hel") # => "lo"
"hello".lchop?("eh")  # => nil
View source

#lchop? : String?

Returns a new String with the first char removed from it if possible, else returns nil.

"hello".lchop? # => "ello"
"".lchop?      # => nil
View source

#lines(chomp = true)

View source

#ljust(len : Int, char : Char = ' ')

Adds instances of char to right of the string until it is at least size of len.

"Purple".ljust(8)      # => "Purple  "
"Purple".ljust(8, '-') # => "Purple--"
"Aubergine".ljust(8)   # => "Aubergine"
View source

#ljust(io : IO, len : Int, char : Char = ' ') : Nil

Adds instances of char to right of the string until it is at least size of len, and then appends the result to the given IO.

io = IO::Memory.new
"Purple".ljust(io, 8, '-')
io.to_s # => "Purple--"
View source

#lstrip(&block : Char -> _)

Returns a new string where leading characters for which the block returns a truthy value are removed.

"bcadefcba".lstrip { |c| 'a' <= c <= 'c' } # => "defcba"
View source

#lstrip

Returns a new String with leading whitespace removed.

"    hello    ".lstrip # => "hello    "
"\tgoodbye\r\n".lstrip # => "goodbye\r\n"
View source

#lstrip(chars : String)

Returns a new string where leading occurrences of any char in chars are removed. The chars argument is not a suffix; rather; all combinations of its values are stripped.

"bcadefcba".lstrip("abc") # => "defcba"
View source

#lstrip(char : Char)

Returns a new string with leading occurrences of char removed.

"aaabcdaaa".lstrip('a') # => "bcdaaa"
View source

#match(regex : Regex, pos = 0) : Regex::MatchData?

Finds match of regex, starting at pos. It also updates $~ with the result.

"foo".match(/foo/) # => Regex::MatchData("foo")
$~                 # => Regex::MatchData("foo")

"foo".match(/bar/) # => nil
$~                 # raises Exception
View source

#matches?(regex : Regex, pos = 0) : Bool

Finds match of regex like #match, but it returns Bool value. It neither returns MatchData nor assigns it to the $~ variable.

"foo".matches?(/bar/) # => false
"foo".matches?(/foo/) # => true

# `$~` is not set even if last match succeeds.
$~ # raises Exception
View source

#partition(search : Char | String) : Tuple(String, String, String)

Searches separator or pattern (Regex) in the string, and returns a Tuple with the part before it, the match, and the part after it. If it is not found, returns str followed by two empty strings.

"hello".partition("l") # => {"he", "l", "lo"}
"hello".partition("x") # => {"hello", "", ""}
View source

#partition(search : Regex) : Tuple(String, String, String)

Searches separator or pattern (Regex) in the string, and returns a Tuple with the part before it, the match, and the part after it. If it is not found, returns str followed by two empty strings.

"hello".partition("l") # => {"he", "l", "lo"}
"hello".partition("x") # => {"hello", "", ""}
View source

#presence : self?

Returns self unless #blank? is true in which case it returns nil.

"a".presence         # => "a"
"".presence          # => nil
"   ".presence       # => nil
"    a    ".presence # => "    a    "
nil.presence         # => nil

config = {"empty" => ""}
config["empty"]?.presence || "default"   # => "default"
config["missing"]?.presence || "default" # => "default"

See also: Nil#presence.

View source

#pretty_print(pp : PrettyPrint) : Nil

Pretty prints self into the given printer.

View source

#rchop(suffix : Char | String) : String

Returns a new String with suffix removed from the end of the string.

"string".rchop('g')   # => "strin"
"string".rchop('x')   # => "string"
"string".rchop("ing") # => "str"
"string".rchop("inx") # => "string"
View source

#rchop : String

Returns a new String with the last character removed. Applying rchop to an empty string returns an empty string.

"string\r\n".rchop # => "string\r"
"string\n\r".rchop # => "string\n"
"string\n".rchop   # => "string"
"string".rchop     # => "strin"
"x".rchop.rchop    # => ""
View source

#rchop?(suffix : Char | String) : String?

Returns a new String with suffix removed from the end of the string if possible, else returns nil.

"string".rchop?('g')   # => "strin"
"string".rchop?('x')   # => nil
"string".rchop?("ing") # => "str"
"string".rchop?("inx") # => nil
View source

#rchop? : String?

Returns a new String with the last character removed if possible, else returns nil.

"string\r\n".rchop? # => "string\r"
"string\n\r".rchop? # => "string\n"
"string\n".rchop?   # => "string"
"string".rchop?     # => "strin"
"".rchop?           # => nil
View source

#reverse

Reverses the order of characters in the string.

"Argentina".reverse # => "anitnegrA"
"racecar".reverse   # => "racecar"
View source

#rindex(search : Regex, offset = size)

Returns the index of the last appearance of search in the string, If offset is present, it defines the position to end the search (characters beyond this point are ignored).

"Hello, World".rindex('o')    # => 8
"Hello, World".rindex('Z')    # => nil
"Hello, World".rindex("o", 5) # => 4
"Hello, World".rindex("W", 2) # => nil
View source

#rindex(search : String, offset = size - search.size)

Returns the index of the last appearance of search in the string, If offset is present, it defines the position to end the search (characters beyond this point are ignored).

"Hello, World".rindex('o')    # => 8
"Hello, World".rindex('Z')    # => nil
"Hello, World".rindex("o", 5) # => 4
"Hello, World".rindex("W", 2) # => nil
View source

#rindex(search : Char, offset = size - 1)

Returns the index of the last appearance of search in the string, If offset is present, it defines the position to end the search (characters beyond this point are ignored).

"Hello, World".rindex('o')    # => 8
"Hello, World".rindex('Z')    # => nil
"Hello, World".rindex("o", 5) # => 4
"Hello, World".rindex("W", 2) # => nil
View source

#rjust(len : Int, char : Char = ' ')

Adds instances of char to left of the string until it is at least size of len.

"Purple".rjust(8)      # => "  Purple"
"Purple".rjust(8, '-') # => "--Purple"
"Aubergine".rjust(8)   # => "Aubergine"
View source

#rjust(io : IO, len : Int, char : Char = ' ') : Nil

Adds instances of char to left of the string until it is at least size of len, and then appends the result to the given IO.

io = IO::Memory.new
"Purple".rjust(io, 8, '-')
io.to_s # => "--Purple"
View source

#rpartition(search : Regex) : Tuple(String, String, String)

Searches separator or pattern (Regex) in the string from the end of the string, and returns a Tuple with the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.

"hello".rpartition("l")  # => {"hel", "l", "o"}
"hello".rpartition("x")  # => {"", "", "hello"}
"hello".rpartition(/.l/) # => {"he", "ll", "o"}
View source

#rpartition(search : Char | String) : Tuple(String, String, String)

Searches separator or pattern (Regex) in the string from the end of the string, and returns a Tuple with the part before it, the match, and the part after it. If it is not found, returns two empty strings and str.

"hello".rpartition("l")  # => {"hel", "l", "o"}
"hello".rpartition("x")  # => {"", "", "hello"}
"hello".rpartition(/.l/) # => {"he", "ll", "o"}
View source

#rstrip(&block : Char -> _)

Returns a new string where trailing characters for which the block returns a truthy value are removed.

"bcadefcba".rstrip { |c| 'a' <= c <= 'c' } # => "bcadef"
View source

#rstrip(char : Char)

Returns a new string with trailing occurrences of char removed.

"aaabcdaaa".rstrip('a') # => "aaabcd"
View source

#rstrip

Returns a new String with trailing whitespace removed.

"    hello    ".rstrip # => "    hello"
"\tgoodbye\r\n".rstrip # => "\tgoodbye"
View source

#rstrip(chars : String)

Returns a new string where trailing occurrences of any char in chars are removed. The chars argument is not a suffix; rather; all combinations of its values are stripped.

"abcdefcba".rstrip("abc") # => "abcdef"
View source

#scan(pattern : Regex

Searches the string for instances of pattern, yielding a Regex::MatchData for each match.

View source

#scan(pattern : Regex)

Searches the string for instances of pattern, returning an Array of Regex::MatchData for each match.

View source

#scan(pattern : String

Searches the string for instances of pattern, yielding the matched string for each match.

View source

#scan(pattern : String)

Searches the string for instances of pattern, returning an array of the matched string for each match.

View source

#scrub(replacement = Char::REPLACEMENT) : String

Returns a String where bytes that are invalid in the UTF-8 encoding are replaced with replacement.

View source

#size

Returns the number of unicode codepoints in this string.

"hello".size # => 5
"你好".size    # => 2
View source

#split(limit : Int32? = nil)

Makes an array by splitting the string on any amount of ASCII whitespace characters (and removing that whitespace).

If limit is present, up to limit new strings will be created, with the entire remainder added to the last string.

old_pond = "
  Old pond
  a frog leaps in
  water's sound
"
old_pond.split    # => ["Old", "pond", "a", "frog", "leaps", "in", "water's", "sound"]
old_pond.split(3) # => ["Old", "pond", "a frog leaps in\n  water's sound\n"]
View source

#split(separator : Regex, limit = nil, *, remove_empty = false, &block : String -> _)

Makes an Array by splitting the string on separator (and removing instances of separator).

If limit is present, the array will be limited to limit items and the final item will contain the remainder of the string.

If separator is an empty regex (//), the string will be separated into one-character strings.

If remove_empty is true, any empty strings are removed from the result.

long_river_name = "Mississippi"
long_river_name.split(/s+/) # => ["Mi", "i", "ippi"]
long_river_name.split(//)   # => ["M", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
View source

#split(separator : String, limit = nil, *, remove_empty = false, &block : String -> _)

Splits the string after each string separator and yields each part to a block.

If limit is present, the array will be limited to limit items and the final item will contain the remainder of the string.

If separator is an empty string (""), the string will be separated into one-character strings.

If remove_empty is true, any empty strings are removed from the result.

ary = [] of String
long_river_name = "Mississippi"

long_river_name.split("ss") { |s| ary << s }
ary # => ["Mi", "i", "ippi"]
ary.clear

long_river_name.split("i") { |s| ary << s }
ary # => ["M", "ss", "ss", "pp", ""]
ary.clear

long_river_name.split("i", remove_empty: true) { |s| ary << s }
ary # => ["M", "ss", "ss", "pp"]
ary.clear

long_river_name.split("") { |s| ary << s }
ary # => ["M", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
View source

#split(limit : Int32? = nil, &block : String -> _)

Splits the string after any amount of ASCII whitespace characters and yields each non-whitespace part to a block.

If limit is present, up to limit new strings will be created, with the entire remainder added to the last string.

ary = [] of String
old_pond = "
  Old pond
  a frog leaps in
  water's sound
"

old_pond.split { |s| ary << s }
ary # => ["Old", "pond", "a", "frog", "leaps", "in", "water's", "sound"]
ary.clear

old_pond.split(3) { |s| ary << s }
ary # => ["Old", "pond", "a frog leaps in\n  water's sound\n"]
View source

#split(separator : Regex, limit = nil, *, remove_empty = false)

Splits the string after each regex separator and yields each part to a block.

If limit is present, the array will be limited to limit items and the final item will contain the remainder of the string.

If separator is an empty regex (//), the string will be separated into one-character strings.

If remove_empty is true, any empty strings are removed from the result.

ary = [] of String
long_river_name = "Mississippi"

long_river_name.split(/s+/) { |s| ary << s }
ary # => ["Mi", "i", "ippi"]
ary.clear

long_river_name.split(//) { |s| ary << s }
ary # => ["M", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
View source

#split(separator : Char, limit = nil, *, remove_empty = false, &block : String -> _)

Splits the string after each character separator and yields each part to a block.

If limit is present, up to limit new strings will be created, with the entire remainder added to the last string.

If remove_empty is true, any empty strings are not yielded.

ary = [] of String

"foo,,bar,baz".split(',') { |string| ary << string }
ary # => ["foo", "", "bar", "baz"]
ary.clear

"foo,,bar,baz".split(',', remove_empty: true) { |string| ary << string }
ary # => ["foo", "bar", "baz"]
ary.clear

"foo,bar,baz".split(',', 2) { |string| ary << string }
ary # => ["foo", "bar,baz"]
View source

#split(separator : String, limit = nil, *, remove_empty = false)

Makes an Array by splitting the string on separator (and removing instances of separator).

If limit is present, the array will be limited to limit items and the final item will contain the remainder of the string.

If separator is an empty string (""), the string will be separated into one-character strings.

If remove_empty is true, any empty strings are removed from the result.

long_river_name = "Mississippi"
long_river_name.split("ss")                    # => ["Mi", "i", "ippi"]
long_river_name.split("i")                     # => ["M", "ss", "ss", "pp", ""]
long_river_name.split("i", remove_empty: true) # => ["M", "ss", "ss", "pp"]
long_river_name.split("")                      # => ["M", "i", "s", "s", "i", "s", "s", "i", "p", "p", "i"]
View source

#split(separator : Char, limit = nil, *, remove_empty = false)

Makes an Array by splitting the string on the given character separator (and removing that character).

If limit is present, up to limit new strings will be created, with the entire remainder added to the last string.

If remove_empty is true, any empty strings are removed from the result.

"foo,,bar,baz".split(',')                     # => ["foo", "", "bar", "baz"]
"foo,,bar,baz".split(',', remove_empty: true) # => ["foo", "bar", "baz"]
"foo,bar,baz".split(',', 2)                   # => ["foo", "bar,baz"]
View source

#squeeze(*sets : String)

Sets should be a list of strings following the rules described at Char#in_set?. Returns a new String with all runs of the same character replaced by one instance, if they match the given set.

If no set is given, all characters are matched.

"aaabbbcccddd".squeeze("b-d") # => "aaabcd"
"a       bbb".squeeze         # => "a b"
View source

#squeeze(char : Char)

Returns a new String, with all runs of char replaced by one instance.

"a    bbb".squeeze(' ') # => "a bbb"
View source

#squeeze

Returns a new String, that has all characters removed, that were the same as the previous one.

"a       bbb".squeeze # => "a b"
View source

#squeeze

Yields each char in this string to the block. Returns a new String, that has all characters removed, that were the same as the previous one and for which the given block returned a truthy value.

"aaabbbccc".squeeze &.in?('a', 'b') # => "abccc"
"aaabbbccc".squeeze &.in?('a', 'c') # => "abbbc"
View source

#starts_with?(re : Regex) : Bool

Returns true if the regular expression re matches at the start of this string.

"22hello".starts_with?(/[0-9]/) # => true
"22hello".starts_with?(/[a-z]/) # => false
"h22".starts_with?(/[a-z]/)     # => true
"h22".starts_with?(/[A-Z]/)     # => false
"h22".starts_with?(/[a-z]{2}/)  # => false
"hh22".starts_with?(/[a-z]{2}/) # => true
View source

#starts_with?(char : Char) : Bool

Returns true if this string starts with the given char.

"hello".starts_with?('h') # => true
"hello".starts_with?('e') # => false
View source

#starts_with?(str : String) : Bool

Returns true if this string starts with the given str.

"hello".starts_with?("h")  # => true
"hello".starts_with?("he") # => true
"hello".starts_with?("hu") # => false
View source

#strip(&block : Char -> _)

Returns a new string where leading and trailing characters for which the block returns a truthy value are removed.

"bcadefcba".strip { |c| 'a' <= c <= 'c' } # => "def"
View source

#strip

Returns a new String with leading and trailing whitespace removed.

"    hello    ".strip # => "hello"
"\tgoodbye\r\n".strip # => "goodbye"
View source

#strip(chars : String)

Returns a new string where leading and trailing occurrences of any char in chars are removed. The chars argument is not a prefix or suffix; rather; all combinations of its values are stripped.

"abcdefcba".strip("abc") # => "def"
View source

#strip(char : Char)

Returns a new string where leading and trailing occurrences of char are removed.

"aaabcdaaa".strip('a') # => "bcd"
View source

#sub(range : Range, replacement : Char)

Returns a new String with characters at the given range replaced by replacement.

"hello".sub(1..2, 'a') # => "halo"
View source

#sub(pattern : Regex, hash : Hash(String, _) | NamedTuple)

Returns a String where the first occurrences of the given pattern is replaced with the matching entry from the hash of replacements. If the first match is not included in the hash, nothing is replaced.

"hello".sub(/(he|l|o)/, {"he": "ha", "l": "la"}) # => "hallo"
"hello".sub(/(he|l|o)/, {"l": "la"})             # => "hello"
View source

#sub(hash : Hash(Char, _))

Returns a String where the first char in the string matching a key in the given hash is replaced by the corresponding hash value.

"hello".sub({'a' => 'b', 'l' => 'd'}) # => "hedlo"
View source

#sub(char : Char, replacement)

Returns a String where the first occurrence of char is replaced by replacement.

"hello".sub('l', "lo")      # => "helolo"
"hello world".sub('o', 'a') # => "hella world"
View source

#sub(index : Int, replacement : String)

Returns a new String with the character at the given index replaced by replacement.

"hello".sub(1, "eee") # => "heeello"
View source

#sub(index : Int, replacement : Char)

Returns a new String with the character at the given index replaced by replacement.

"hello".sub(1, 'a') # => "hallo"
View source

#sub(&block : Char -> _)

Returns a new String where the first character is yielded to the given block and replaced by its return value.

"hello".sub { |char| char + 1 } # => "iello"
"hello".sub { "hi" }            # => "hiello"
View source

#sub(pattern : Regex, replacement, backreferences = true)

Returns a String where the first occurrence of pattern is replaced by replacement

"hello".sub(/[aeiou]/, "*") # => "h*llo"

Within replacement, the special match variable $~ will not refer to the current match.

If backreferences is true (the default value), replacement can include backreferences:

"hello".sub(/[aeiou]/, "(\\0)") # => "h(e)llo"

When substitution is performed, any backreferences found in replacement will be replaced with the contents of the corresponding capture group in pattern. Backreferences to capture groups that were not present in pattern or that did not match will be skipped. See Regex for information about capture groups.

Backreferences are expressed in the form "\\d", where d is a group number, or "\\k&lt;name>" where name is the name of a named capture group. A sequence of literal characters resembling a backreference can be expressed by placing "\\" before the sequence.

"foo".sub(/o/, "x\\0x")                  # => "fxoxo"
"foofoo".sub(/(?<bar>oo)/, "|\\k<bar>|") # => "f|oo|foo"
"foo".sub(/o/, "\\\\0")                  # => "f\\0o"

Raises ArgumentError if an incomplete named back-reference is present in replacement.

Raises IndexError if a named group referenced in replacement is not present in pattern.

View source

#sub(range : Range, replacement : String)

Returns a new String with characters at the given range replaced by replacement.

"hello".sub(1..2, "eee") # => "heeelo"
View source

#sub(pattern : Regex

Returns a String where the first occurrence of pattern is replaced by the block's return value.

"hello".sub(/./) { |s| s[0].ord.to_s + ' ' } # => "104 ello"
View source

#sub(string : String

Returns a String where the first occurrences of the given string is replaced with the block's value.

"hello yellow".sub("ll") { "dd" } # => "heddo yellow"
View source

#sub(string : String, replacement)

Returns a String where the first occurrences of the given string is replaced with the given replacement.

"hello yellow".sub("ll", "dd") # => "heddo yellow"
View source

#succ

Returns the successor of the string. The successor is calculated by incrementing characters starting from the rightmost alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always results in another digit, and incrementing a letter results in another letter of the same case.

If the increment generates a "carry", the character to the left of it is incremented. This process repeats until there is no carry, adding an additional character if necessary.

"abcd".succ      # => "abce"
"THX1138".succ   # => "THX1139"
"((koala))".succ # => "((koalb))"
"1999zzz".succ   # => "2000aaa"
"ZZZ9999".succ   # => "AAAA0000"
"***".succ       # => "**+"
View source

#titleize(io : IO, options : Unicode::CaseOptions = :none) : Nil

Writes a titleized version of self to the given io.

io = IO::Memory.new
"x-men: the last stand".titleize io
io.to_s # => "X-men: The Last Stand"
View source

#titleize(options : Unicode::CaseOptions = :none) : String

Returns a new String with the first letter after any space converted to uppercase and every other letter converted to lowercase.

"hEllO tAb\tworld".titleize      # => "Hello Tab\tWorld"
"  spaces before".titleize       # => "  Spaces Before"
"x-men: the last stand".titleize # => "X-men: The Last Stand"
View source

#to_big_d

Converts self to BigDecimal.

require "big"
"1212341515125412412412421".to_big_d

View source

#to_big_f

Converts self to a BigFloat.

require "big"
"1234.0".to_big_f
View source

#to_big_i(base = 10) : BigInt

Returns a BigInt from this string, in the given base.

Raises ArgumentError if this string doesn't denote a valid integer.

require "big"

"3a060dbf8d1a5ac3e67bc8f18843fc48".to_big_i(16)

View source

#to_f(whitespace : Bool = true, strict : Bool = true)

Returns the result of interpreting characters in this string as a floating point number (Float64). This method raises an exception if the string is not a valid float representation or exceeds the range of the data type. Values representing infinity or NaN are considered valid.

Options: * whitespace: if true, leading and trailing whitespaces are allowed * strict: if true, extraneous characters past the end of the number are disallowed

"123.45e1".to_f                # => 1234.5
"45.67 degrees".to_f           # raises ArgumentError
"thx1138".to_f(strict: false)  # raises ArgumentError
" 1.2".to_f(whitespace: false) # raises ArgumentError
"1.2foo".to_f(strict: false)   # => 1.2
View source

#to_f32(whitespace : Bool = true, strict : Bool = true)

Same as #to_f but returns a Float32.

View source

#to_f32?(whitespace : Bool = true, strict : Bool = true)

Same as #to_f? but returns a Float32.

View source

#to_f64(whitespace : Bool = true, strict : Bool = true)

Returns the result of interpreting characters in this string as a floating point number (Float64). This method raises an exception if the string is not a valid float representation or exceeds the range of the data type. Values representing infinity or NaN are considered valid.

Options: * whitespace: if true, leading and trailing whitespaces are allowed * strict: if true, extraneous characters past the end of the number are disallowed

"123.45e1".to_f                # => 1234.5
"45.67 degrees".to_f           # raises ArgumentError
"thx1138".to_f(strict: false)  # raises ArgumentError
" 1.2".to_f(whitespace: false) # raises ArgumentError
"1.2foo".to_f(strict: false)   # => 1.2
View source

#to_f64?(whitespace : Bool = true, strict : Bool = true)

Returns the result of interpreting characters in this string as a floating point number (Float64). This method returns nil if the string is not a valid float representation or exceeds the range of the data type. Values representing infinity or NaN are considered valid.

Options: * whitespace: if true, leading and trailing whitespaces are allowed * strict: if true, extraneous characters past the end of the number are disallowed

"123.45e1".to_f?                # => 1234.5
"45.67 degrees".to_f?           # => nil
"thx1138".to_f?                 # => nil
" 1.2".to_f?(whitespace: false) # => nil
"1.2foo".to_f?(strict: false)   # => 1.2
View source

#to_f?(whitespace : Bool = true, strict : Bool = true)

Returns the result of interpreting characters in this string as a floating point number (Float64). This method returns nil if the string is not a valid float representation or exceeds the range of the data type. Values representing infinity or NaN are considered valid.

Options: * whitespace: if true, leading and trailing whitespaces are allowed * strict: if true, extraneous characters past the end of the number are disallowed

"123.45e1".to_f?                # => 1234.5
"45.67 degrees".to_f?           # => nil
"thx1138".to_f?                 # => nil
" 1.2".to_f?(whitespace: false) # => nil
"1.2foo".to_f?(strict: false)   # => 1.2
View source

#to_i(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i, but returns the block's value if there is not a valid number at the start of this string, or if the resulting integer doesn't fit an Int32.

"12345".to_i { 0 } # => 12345
"hello".to_i { 0 } # => 0
View source

#to_i(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false)

Returns the result of interpreting leading characters in this string as an integer base base (between 2 and 36).

If there is not a valid number at the start of this string, or if the resulting integer doesn't fit an Int32, an ArgumentError is raised.

Options: * whitespace: if true, leading and trailing whitespaces are allowed * underscore: if true, underscores in numbers are allowed * prefix: if true, the prefixes "0x", "0o" and "0b" override the base * strict: if true, extraneous characters past the end of the number are disallowed * leading_zero_is_octal: if true, then a number prefixed with "0" will be treated as an octal

"12345".to_i             # => 12345
"0a".to_i                # raises ArgumentError
"hello".to_i             # raises ArgumentError
"0a".to_i(16)            # => 10
"1100101".to_i(2)        # => 101
"1100101".to_i(8)        # => 294977
"1100101".to_i(10)       # => 1100101
"1100101".to_i(base: 16) # => 17826049

"12_345".to_i                   # raises ArgumentError
"12_345".to_i(underscore: true) # => 12345

"  12345  ".to_i                    # => 12345
"  12345  ".to_i(whitespace: false) # raises ArgumentError

"0x123abc".to_i               # raises ArgumentError
"0x123abc".to_i(prefix: true) # => 1194684

"99 red balloons".to_i                # raises ArgumentError
"99 red balloons".to_i(strict: false) # => 99

"0755".to_i                              # => 755
"0755".to_i(leading_zero_is_octal: true) # => 493
View source

#to_i16(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int16

Same as #to_i but returns an Int16.

View source

#to_i16(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an Int16 or the block's value.

View source

#to_i16?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int16?

Same as #to_i but returns an Int16 or nil.

View source

#to_i32(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i.

View source

#to_i32(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int32

Same as #to_i.

View source

#to_i32?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int32?

Same as #to_i.

View source

#to_i64(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an Int64 or the block's value.

View source

#to_i64(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int64

Same as #to_i but returns an Int64.

View source

#to_i64?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int64?

Same as #to_i but returns an Int64 or nil.

View source

#to_i8(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int8

Same as #to_i but returns an Int8.

View source

#to_i8(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an Int8 or the block's value.

View source

#to_i8?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : Int8?

Same as #to_i but returns an Int8 or nil.

View source

#to_i?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false)

Same as #to_i, but returns nil if there is not a valid number at the start of this string, or if the resulting integer doesn't fit an Int32.

"12345".to_i?             # => 12345
"99 red balloons".to_i?   # => nil
"0a".to_i?(strict: false) # => 0
"hello".to_i?             # => nil
View source

#to_json(json : JSON::Builder)

View source

#to_json_object_key

View source

#to_s(io : IO) : Nil

Appends self to io.

View source

#to_s : String

Returns self.

View source

#to_slice : Bytes

Returns the underlying bytes of this String.

The returned slice is read-only.

View source

#to_u16(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt16

Same as #to_i but returns an UInt16.

View source

#to_u16(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an UInt16 or the block's value.

View source

#to_u16?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt16?

Same as #to_i but returns an UInt16 or nil.

View source

#to_u32(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt32

Same as #to_i but returns an UInt32.

View source

#to_u32(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an UInt32 or the block's value.

View source

#to_u32?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt32?

Same as #to_i but returns an UInt32 or nil.

View source

#to_u64(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt64

Same as #to_i but returns an UInt64.

View source

#to_u64(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an UInt64 or the block's value.

View source

#to_u64?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt64?

Same as #to_i but returns an UInt64 or nil.

View source

#to_u8(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt8

Same as #to_i but returns an UInt8.

View source

#to_u8(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false

Same as #to_i but returns an UInt8 or the block's value.

View source

#to_u8?(base : Int = 10, whitespace : Bool = true, underscore : Bool = false, prefix : Bool = false, strict : Bool = true, leading_zero_is_octal : Bool = false) : UInt8?

Same as #to_i but returns an UInt8 or nil.

View source

#to_unsafe : Pointer(UInt8)

Returns a pointer to the underlying bytes of this String.

View source

#to_utf16 : Slice(UInt16)

Returns the UTF-16 encoding of the given string.

Invalid chars (in the range U+D800..U+DFFF) are encoded with the unicode replacement char value 0xfffd.

The byte following the end of this slice (but not included in it) is defined to be zero. This allows passing the result of this function into C functions that expect a null-terminated UInt16*.

"hi 𐂥".to_utf16 # => Slice[104_u16, 105_u16, 32_u16, 55296_u16, 56485_u16]
View source

#tr(from : String, to : String)

Returns a new string _tr_anslating characters using from and to as a map. If to is shorter than from, the last character in to is used for the rest. If to is empty, this acts like String#delete.

"aabbcc".tr("abc", "xyz") # => "xxyyzz"
"aabbcc".tr("abc", "x")   # => "xxxxxx"
"aabbcc".tr("a", "xyz")   # => "xxbbcc"
View source

#underscore(io : IO, options : Unicode::CaseOptions = :none) : Nil

Writes an underscored version of self to the given io.

io = IO::Memory.new
"DoesWhatItSaysOnTheTin".underscore io
io.to_s # => "does_what_it_says_on_the_tin"
View source

#underscore(options : Unicode::CaseOptions = :none) : String

Converts camelcase boundaries to underscores.

"DoesWhatItSaysOnTheTin".underscore                         # => "does_what_it_says_on_the_tin"
"PartyInTheUSA".underscore                                  # => "party_in_the_usa"
"HTTP_CLIENT".underscore                                    # => "http_client"
"3.14IsPi".underscore                                       # => "3.14_is_pi"
"InterestingImage".underscore(Unicode::CaseOptions::Turkic) # => "ınteresting_ımage"
View source

#unsafe_byte_at(index : Int) : UInt8

Returns the byte at the given index without bounds checking.

View source

#unsafe_byte_slice(byte_offset, count) : Slice

Returns count of underlying bytes of this String starting at given byte_offset.

The returned slice is read-only.

View source

#unsafe_byte_slice(byte_offset) : Slice

Returns the underlying bytes of this String starting at given byte_offset.

The returned slice is read-only.

View source

#upcase(io : IO, options : Unicode::CaseOptions = :none) : Nil

Writes a upcased version of self to the given io.

io = IO::Memory.new
"hEllO".upcase io
io.to_s # => "HELLO"
View source

#upcase(options : Unicode::CaseOptions = :none) : String

Returns a new String with each lowercase letter replaced with its uppercase counterpart.

"hEllO".upcase # => "HELLO"
View source

#valid_encoding?

Returns true if this String is encoded correctly according to the UTF-8 encoding.

View source