rubyguides

Reference

String Methods

Methods available on Ruby String objects.

  1. force_encoding

    Re-label a string's encoding without altering its underlying bytes. Changes how Ruby interprets the string's content.

  2. String#bytes

    Returns an array of integers representing the raw byte values of a string. Encoding-aware and essential for binary data work.

  3. String#bytesize

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

  4. String#byteslice

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

  5. String#capitalize

    Returns a copy of the string with its first character uppercased and the rest lowercased.

  6. String#center

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

  7. String#chars

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

  8. String#chomp

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

  9. String#chomp

    Remove trailing line separators from a string. Handles CR, LF, and CRLF, plus custom separators. Nil and empty separators have special behavior.

  10. String#chop

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

  11. 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.

  12. String#chop!

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

  13. String#clone

    Create a shallow copy of a string, preserving its frozen status and singleton methods.

  14. String#concat

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

  15. String#count

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

  16. String#crypt

    Encrypts a string using POSIX crypt(3) with a salt. Deprecated in Ruby 2.5+ — use string-crypt gem instead.

  17. String#delete

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

  18. String#downcase

    Returns a new string with all characters converted to lowercase.

  19. String#downcase

    Returns a new string with all characters converted to lowercase.

  20. 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.

  21. String#each_line

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

  22. String#empty?

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

  23. String#encode

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

  24. String#encoding

    Returns the Encoding object that represents the character encoding of the string.

  25. String#end_with?

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

  26. String#end_with?

    Checks if a string ends with a given suffix or any of several suffixes.

  27. String#freeze

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

  28. String#gsub

    Replace all occurrences of a pattern in a string. Works with strings, regexes, hashes, and blocks. Returns a new string.

  29. String#gsub!

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

  30. String#hex

    Converts the leading hex substring of a string to an integer, accepting optional 0x prefix.

  31. String#include?

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

  32. String#include?

    Check if a string contains a given substring. Returns true or false based on whether the substring is found.

  33. String#index

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

  34. String#insert

    Insert a string at a given index. Accepts negative indices to count from the end.

  35. String#length

    Returns the number of characters in a string.

  36. 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.

  37. String#ljust

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

  38. String#lstrip

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

  39. String#match

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

  40. String#match

    Search a string for a pattern and return a MatchData object. Takes a regexp or string pattern and optional start position.

  41. String#match?

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

  42. String#next

    Return the successor of a string — the next character in sequence. Also aliased as succ.

  43. 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

  44. String#partition

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

  45. String#prepend

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

  46. String#replace

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

  47. String#replace

    Replaces a string's contents in place, returning self and preserving object identity.

  48. String#reverse

    Returns a new string with the characters in reverse order.

  49. String#reverse

    Reverse a string in Ruby with the reverse method. Also covers reverse! for in-place reversal and use cases like palindrome checks.

  50. String#rindex

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

  51. String#rjust

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

  52. String#rstrip

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

  53. String#scan

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

  54. 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.

  55. String#slice

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

  56. String#split

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

  57. String#split

    Split a string into an array of substrings by a delimiter. Supports regex patterns, limits, and special empty-string handling.

  58. String#squeeze

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

  59. String#start_with?

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

  60. 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.

  61. String#strip

    Removes both leading and trailing whitespace from a string.

  62. String#sub

    Replace the first occurrence of a pattern in a string. Use a string, regexp, or block to compute the replacement.

  63. String#sub

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

  64. String#succ

    Returns the next lexicographic string after the current one, incrementing characters from right to left with carry propagation.

  65. String#swapcase

    Returns a new string with the case of each character swapped — uppercase becomes lowercase and vice versa.

  66. String#swapcase

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

  67. String#to_i

    Converts string to integer using provided base.

  68. 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.

  69. String#tr

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

  70. String#unicode_normalize

    Normalize Unicode strings to a consistent form (NFC, NFD, NFKC, NFKD) for reliable comparison and storage in Ruby.

  71. String#unpack

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

  72. String#unpack1

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

  73. String#upcase

    Convert a Ruby string to uppercase using Unicode-aware case mapping.

  74. String#upcase

    Returns a new string with all characters converted to uppercase.

  75. String#valid_encoding?

    Returns true if the string bytes are valid for its current encoding, false otherwise.