Re-label a string's encoding without altering its underlying bytes. Changes how Ruby interprets the string's content.
Reference
String Methods
Methods available on Ruby String objects.
- force_encoding
- String#bytes
Returns an array of integers representing the raw byte values of a string. Encoding-aware and essential for binary data work.
- String#bytesize
Returns the number of bytes in a string's encoding.
- String#byteslice
Extracts a substring by byte index. Unlike slice, byteslice works with byte positions instead of character positions.
- String#capitalize
Returns a copy of the string with its first character uppercased and the rest lowercased.
- String#center
Pad a string to a specified width with the given filler, centering the original string.
- String#chars
Returns an array of characters in the string, where each character is a new string element.
- String#chomp
Removes the trailing record separator (newline, carriage return) from a string. Returns a new string with the separator removed.
- String#chomp
Remove trailing line separators from a string. Handles CR, LF, and CRLF, plus custom separators. Nil and empty separators have special behavior.
- String#chop
Returns a new string with the last character removed, without modifying the original.
- String#chop
Remove the last character from a string, or the last CRLF pair. Unlike chomp, chop removes any character unconditionally. The bang variant modifies in place.
- String#chop!
Removes the last character from the receiver string in place, returning the modified string.
- String#clone
Create a shallow copy of a string, preserving its frozen status and singleton methods.
- String#concat
Concatenates one or more strings onto the original string. The + operator is an alias for this method.
- String#count
Counts the number of occurrences of each character or substring in a string. Returns an integer representing the total count.
- String#crypt
Encrypts a string using POSIX crypt(3) with a salt. Deprecated in Ruby 2.5+ — use string-crypt gem instead.
- String#delete
Removes characters from a string. Pass one or more character sets to delete; returns a new string with all matching characters removed.
- String#downcase
Returns a new string with all characters converted to lowercase.
- String#downcase
Returns a new string with all characters converted to lowercase.
- 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.
- String#each_line
Iterates over each line in a string or IO object, yielding each line as a string.
- String#empty?
Checks if a string has zero length. Returns true for empty strings and false otherwise.
- String#encode
Converts the encoding of a string to the specified encoding, returning a new string with the characters re-encoded.
- String#encoding
Returns the Encoding object that represents the character encoding of the string.
- String#end_with?
Checks if a string ends with a given substring or any of multiple substrings. Returns true or false.
- String#end_with?
Checks if a string ends with a given suffix or any of several suffixes.
- String#freeze
Prevents further modification of an object by locking it for changes, making it immutable for the object's lifetime.
- String#gsub
Replace all occurrences of a pattern in a string. Works with strings, regexes, hashes, and blocks. Returns a new string.
- String#gsub!
Replaces all occurrences of a pattern in place, modifying the original string.
- String#hex
Converts the leading hex substring of a string to an integer, accepting optional 0x prefix.
- String#include?
Check if a string contains a substring. Returns true or false.
- String#include?
Check if a string contains a given substring. Returns true or false based on whether the substring is found.
- String#index
Finds the position of a substring within a string. index searches from the left, rindex searches from the right.
- String#insert
Insert a string at a given index. Accepts negative indices to count from the end.
- String#length
Returns the number of characters in a string.
- String#lines in Ruby — Split String Lines Into an Array
How to split string lines in Ruby using String#lines. Covers chomp, custom separators, paragraph mode, line ending handling, and gotchas.
- String#ljust
Pad a string to a specified width on the right with the given filler, left-justifying the original string.
- String#lstrip
Removes leading and/or trailing whitespace from a string. lstrip removes left (leading), rstrip removes right (trailing), strip removes both.
- String#match
Matches a string against a regex pattern, returning MatchData or nil. Use match? for boolean results.
- String#match
Search a string for a pattern and return a MatchData object. Takes a regexp or string pattern and optional start position.
- String#match?
Returns true if a regex matches the string, or if a pattern matches an object, without creating match data.
- String#next
Return the successor of a string — the next character in sequence. Also aliased as succ.
- String#oct
Converts a string to an integer by interpreting leading characters as octal. Handles 0b/0x prefixes, negative numbers, and stops at the first non-octal digit
- String#partition
Divides a string into three parts at a separator. Returns [before, separator, after] as a 3-element array.
- 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.
- String#replace
Replaces a string's contents in place, returning self and preserving object identity.
- String#reverse
Returns a new string with the characters in reverse order.
- String#reverse
Reverse a string in Ruby with the reverse method. Also covers reverse! for in-place reversal and use cases like palindrome checks.
- String#rindex
Returns the index of the last occurrence of a substring or pattern in a string.
- String#rjust
Pad a string to a specified width on the left with the given filler, right-justifying the original string.
- String#rstrip
Removes trailing whitespace from a string, including spaces, tabs, and newlines.
- String#scan
Scans a string for all occurrences of a pattern and returns an array of matched substrings.
- String#scan
Find all occurrences of a pattern in a string, returning an array of matches. Use a block to process each match without building an intermediate array.
- String#slice
Extracts a substring from a string using integer index, range, or regular expression.
- String#split
Splits a string into an array of substrings based on a delimiter pattern.
- String#split
Split a string into an array of substrings by a delimiter. Supports regex patterns, limits, and special empty-string handling.
- String#squeeze
Returns a new string with consecutive repeated characters collapsed to a single character. Optionally squeeze specific characters only.
- String#start_with?
Checks if a string begins with a given prefix or any of several prefixes.
- String#strip
Remove leading and trailing whitespace from a string. Also removes null bytes, form feeds, and tabs. Use lstrip or rstrip to remove from one side only.
- String#strip
Removes both leading and trailing whitespace from a string.
- String#sub
Replace the first occurrence of a pattern in a string. Use a string, regexp, or block to compute the replacement.
- String#sub
Replaces the first occurrence of a pattern in a string. Returns a new string with only the first match replaced.
- String#succ
Returns the next lexicographic string after the current one, incrementing characters from right to left with carry propagation.
- String#swapcase
Returns a new string with the case of each character swapped — uppercase becomes lowercase and vice versa.
- String#swapcase
Returns a copy of the string with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
- String#to_i
Converts string to integer using provided base.
- 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.
- String#tr
Replaces characters in a string according to a mapping from from_str to to_str. Returns a new string.
- String#unicode_normalize
Normalize Unicode strings to a consistent form (NFC, NFD, NFKC, NFKD) for reliable comparison and storage in Ruby.
- String#unpack
Decodes a binary string according to a template format, returning an array of values (or a single value for unpack1).
- String#unpack1
Returns the first element of a packed string interpretation, converting binary data to Ruby values.
- String#upcase
Convert a Ruby string to uppercase using Unicode-aware case mapping.
- String#upcase
Returns a new string with all characters converted to uppercase.
- String#valid_encoding?
Returns true if the string bytes are valid for its current encoding, false otherwise.