← Reference

String Methods

Methods available on Ruby String objects.

String#bytesize

Returns the number of bytes in a string's encoding.

str.bytesize -> integer

String#byteslice

Extracts a substring by byte index. Unlike slice, byteslice works with byte positions instead of character positions.

str.byteslice(index) or str.byteslice(start, length)

String#center

Pad a string to a specified width with the given filler, centering the original string.

str.center(width, padstr = ' ') -> string

String#chars

Returns an array of characters in the string, where each character is a new string element.

str.chars -> array

String#chomp

Removes the trailing record separator (newline, carriage return) from a string. Returns a new string with the separator removed.

str.chomp(separator)

String#chop

Returns a new string with the last character removed, without modifying the original.

str.chop -> new_string

String#chop!

Removes the last character from the receiver string in place, returning the modified string.

str.chop! -> str or nil

String#concat

Concatenates one or more strings onto the original string. The + operator is an alias for this method.

str.concat(other)

String#count

Counts the number of occurrences of each character or substring in a string. Returns an integer representing the total count.

str.count([other_str]+) -> integer

String#delete

Removes characters from a string. Pass one or more character sets to delete; returns a new string with all matching characters removed.

str.delete([other_str]+) -> string

String#downcase

Returns a new string with all characters converted to lowercase.

str.downcase -> string

String#each_char

Iterate over each character or line in a Ruby string. each_char yields single characters while each_line splits by line separators.

str.each_char { |char| block } -> str

String#each_line

Iterates over each line in a string or IO object, yielding each line as a string.

str.each_line(separator=$/) { |line| block } -> str

String#empty?

Checks if a string has zero length. Returns true for empty strings and false otherwise.

str.empty? -> true or false

String#encode

Converts the encoding of a string to the specified encoding, returning a new string with the characters re-encoded.

str.encode(encoding) -> string

String#end_with?

Checks if a string ends with a given substring or any of multiple substrings. Returns true or false.

str.end_with?(*suffixes) -> true or false

String#freeze

Prevents further modification of an object by locking it for changes, making it immutable for the object's lifetime.

str.freeze -> str

String#gsub!

Replaces all occurrences of a pattern in place, modifying the original string.

str.gsub!(pattern, replacement) -> str or nil

String#include?

Check if a string contains a substring. Returns true or false.

String#index

Finds the position of a substring within a string. index searches from the left, rindex searches from the right.

str.index(substr, offset) -> integer or nil

String#length

Returns the number of characters in a string.

str.length -> integer

String#ljust

Pad a string to a specified width on the right with the given filler, left-justifying the original string.

str.ljust(width, padstr = ' ') -> string

String#lstrip

Removes leading and/or trailing whitespace from a string. lstrip removes left (leading), rstrip removes right (trailing), strip removes both.

str.strip -> string

String#match

Matches a string against a regex pattern, returning MatchData or nil. Use match? for boolean results.

str.match(pattern) -> MatchData or nil

String#match?

Returns true if a regex matches the string, or if a pattern matches an object, without creating match data.

regexp.match?(string) -> true or false

String#partition

Divides a string into three parts at a separator. Returns [before, separator, after] as a 3-element array.

str.partition(sep)

String#prepend

Prepends one or more strings to the beginning of the target string. Modifies the string in place and returns self.

String#replace

Replaces the entire contents of a string with another string, modifying the original string in place.

str.replace(other_str) -> str

String#reverse

Returns a new string with the characters in reverse order.

str.reverse -> string

String#rindex

Returns the index of the last occurrence of a substring or pattern in a string.

str.rindex(substring [, offset]) -> integer or nil

String#rjust

Pad a string to a specified width on the left with the given filler, right-justifying the original string.

str.rjust(width, padstr = ' ') -> string

String#rstrip

Removes trailing whitespace from a string, including spaces, tabs, and newlines.

str.rstrip -> new_string

String#scan

Scans a string for all occurrences of a pattern and returns an array of matched substrings.

str.scan(pattern) -> array

String#slice

Extracts a substring from a string using integer index, range, or regular expression.

str.slice(index) -> string or nil str.slice(start, length) -> string or nil str.slice(range) -> string or nil str.slice(regexp) -> string or nil str.slice(regexp, capture) -> string or nil

String#split

Splits a string into an array of substrings based on a delimiter pattern.

str.split(pattern=nil, limit=nil) -> array

String#squeeze

Returns a new string with consecutive repeated characters collapsed to a single character. Optionally squeeze specific characters only.

str.squeeze([other_str]*) -> string

String#start_with?

Checks if a string begins with a given prefix or any of several prefixes.

str.start_with?(*prefixes) -> true or false

String#strip

Removes both leading and trailing whitespace from a string.

str.strip -> string

String#sub

Replaces the first occurrence of a pattern in a string. Returns a new string with only the first match replaced.

str.sub(pattern, replacement) -> string

String#swapcase

Returns a copy of the string with uppercase characters converted to lowercase and lowercase characters converted to uppercase.

str.swapcase -> string

String#to_i

Converts string to integer using provided base.

str.to_i(base=10) -> class-integer-class

String#to_sym

Converts a string to a symbol. Returns the symbol corresponding to the string content, or creates a new symbol if it doesn't already exist.

str.to_sym -> symbol

String#tr

Replaces characters in a string according to a mapping from from_str to to_str. Returns a new string.

str.tr(from_str, to_str) -> string

String#unpack

Decodes a binary string according to a template format, returning an array of values (or a single value for unpack1).

str.unpack(template) -> array | str.unpack1(template) -> value

String#unpack1

Returns the first element of a packed string interpretation, converting binary data to Ruby values.

str.unpack1(format) -> object

String#upcase

Returns a new string with all characters converted to uppercase.

str.upcase -> string