Some String methods.

Formatting

capitalize

the first character is converted to uppercase and all others to lowercase

 "hello".capitalize   # => "Hello"
 "Hello".capitalize   # => "Hello"
 "hELLO".capitalize   # => "Hello"
" HELLO".capitalize   # => " hello"
"1HELLO".capitalize   # => "1hello"

center

center( width, padding=” “ )

if width is greater than the length of the string, then the string is centered between the given padding

 "hello".center( 8 )          # => " hello  "
 "hello".center( 8, "-" )     # => "-hello--"
 "hello".center( 9 )          # => "  hello  "
    "12".center( 5, "0" )     # => "01200"
  "1011".center( 8, "0" )     # => "00101100"
 "hello".center( 0 )          # => "hello"
 "hello".center( 3 )          # => "hello"
 "hello".center( 4 )          # => "hello"
  "ABCD".center( 9, "xyz" )   # => "xyABCDxyz"

see also ljust and rjust

chomp

chomp( separator=$/ )

the given record separator is removed from the end of string
if $/ has not been changed from the default Ruby record separator, then chomp also removes carriage return characters (that is it will remove \n, \r, and \r\n)

    "hello".chomp   # => "hello"
  "hello\n".chomp   # => "hello"
  "hello\r".chomp   # => "hello"
"hello\r\n".chomp   # => "hello"
"hello\n\r".chomp   # => "hello\n"

if the separator ($/) is an empty string, it will remove all trailing newlines from the string

    "hello".chomp( "" )   # => "hello"
  "hello\n".chomp( "" )   # => "hello"
  "hello\r".chomp( "" )   # => "hello\r"
"hello\r\n".chomp( "" )   # => "hello"
"hello\n\r".chomp( "" )   # => "hello\n\r"

remove any character sequence (string) you want from the end of the orignal string

    "hello".chomp( "?!" )   # => "hello"
  "hello?!".chomp( "?!" )   # => "hello"
"hello?!?!".chomp( "?!" )   # => "hello?!"
  "hello!?".chomp( "?!" )   # => "hello!?"
  "hello??".chomp( "?!" )   # => "hello??"
  "hello!!".chomp( "?!" )   # => "hello!!"
    "hello".chomp( "?" )    # => "hello"
   "hello?".chomp( "?" )    # => "hello"
  "hello??".chomp( "?" )    # => "hello?"

chop

remove the last character
if the string ends with \r\n, both characters are removed
applying chop to an empty string returns an empty string

    "hello".chop   # => "hell"
  "hello\n".chop   # => "hello"
  "hello\r".chop   # => "hello"
"hello\r\n".chop   # => "hello"
"hello\n\r".chop   # => "hello\n"
         "".chop   # => ""

chomp is often a safer alternative, as it leaves the string unchanged if it doesn’t end with the record separator

downcase

replace all uppercase letters with their lowercase counterparts

  "hello".downcase   # => "hello"
  "HELLO".downcase   # => "hello"
  "Hello".downcase   # => "hello"
  "heLLO".downcase   # => "hello"
" heLLO ".downcase   # => " hello "

see also upcase and swapcase

ljust

ljust( width, padding=” “ )

left justify - if width is greater than the length of the string, then the string is left justified and padded with copies of padding

 "hello".ljust( 8 )          # => "hello   "
 "hello".ljust( 8, "-" )     # => "hello---"
    "12".ljust( 5, "0" )     # => "12000"
  "1011".ljust( 8, "0" )     # => "10110000"
 "hello".ljust( 0 )          # => "hello"
 "hello".ljust( 3 )          # => "hello"
 "hello".ljust( 4 )          # => "hello"
  "ABCD".ljust( 9, "xyz" )   # => "ABCDxyzxy"

see also rjust and center

lstrip

left strip - remove leading whitespace

        "  hello".lstrip   # => "hello"
        "hello  ".lstrip   # => "hello  "
      "  hello  ".lstrip   # => "hello  "
         "\r\t\n".lstrip   # => ""
"\r\t\nhello\v\f".lstrip   # => "hello\v\f"

see also strip and rstrip

rjust

rjust( width, padding=” “ )

right justify - if width is greater than the length of the string, then the string is right justified and padded with copies of padding

 "hello".rjust( 8 )          # => "   hello"
 "hello".rjust( 8, "-" )     # => "---hello"
    "12".rjust( 5, "0" )     # => "00012"
  "1011".rjust( 8, "0" )     # => "00001011"
 "hello".rjust( 0 )          # => "hello"
 "hello".rjust( 3 )          # => "hello"
 "hello".rjust( 4 )          # => "hello"
  "ABCD".rjust( 9, "xyz" )   # => "xyzxyABCD"

see also ljust and center

rstrip

right strip - remove trailing whitespace

        "  hello".rstrip   # => "  hello"
        "hello  ".rstrip   # => "hello"
      "  hello  ".rstrip   # => "  hello"
         "\r\t\n".rstrip   # => ""
"\r\t\nhello\v\f".rstrip   # => "\r\t\nhello"

remove also trailing NUL characters

    "  hello\000".rstrip   # => "  hello"
  "  hello\u0000".rstrip   # => "  hello"
 "  hello\u0000 ".rstrip   # => "  hello"
 "  hello \u0000".rstrip   # => "  hello"

see also strip and lstrip

strip

remove leading and trailing whitespace

        "  hello".strip   # => "hello"
        "hello  ".strip   # => "hello"
      "  hello  ".strip   # => "hello"
         "\r\t\n".strip   # => ""
"\r\t\nhello\v\f".strip   # => "hello"

see also lstrip and rstrip

swapcase

replace all lowercase letters with their uppercase counterparts and all uppercase letters with their lowercase counterparts

  "hello".swapcase   # => "HELLO"
  "HELLO".swapcase   # => "hello"
  "Hello".swapcase   # => "hELLO"
  "heLLO".swapcase   # => "HEllo"
" heLLO ".swapcase   # => " HEllo "

see also downcase and upcase

upcase

replace all lowercase letters with their uppercase counterparts

  "hello".upcase   # => "HELLO"
  "HELLO".upcase   # => "HELLO"
  "Hello".upcase   # => "HELLO"
  "heLLO".upcase   # => "HELLO"
" heLLO ".upcase   # => " HELLO "

see also downcase and swapcase

Basic methods

chars

returns an array of characters in string

        "".chars   # => []
      "12".chars   # => ["1", "2"]
   "hello".chars   # => ["h", "e", "l", "l", "o"]
"  hello ".chars   # => [" ", " ", "h", "e", "l", "l", "o", " "]

empty?

returns true if string has a length of zero

     "".empty?   # => true
"hello".empty?   # => false
    " ".empty?   # => false

length

returns the character length of the string

     "".length   # => 0
   "12".length   # => 2
"hello".length   # => 5

Interpolation

string interpolation

                                               "1 + 1 is #{ 1+1 }"   # => "1 + 1 is 2"
                        "This is an interpolation test: #{ "ok" }"   # => "This is an interpolation test: ok"
"Is recursive (nested) interpolation possible?: #{ "#{ "yes" }" }"   # => "Is recursive (nested) interpolation possible?: yes"