Last Updated: February 25, 2016
·
7.21K
· dennismonsewicz

Ruby: Extracting only uppercase letters from a string

Recently I was working on a project that called for me to extract the uppercase letters from a string and then in one or another, manipulate these letters.

So, to easily do this, I simply extending the String Ruby object by doing the following

class String
  def extract_upper_case_letters
    self.scan /\p{Upper}/
  end
end

# Implementation
str = "Hello World"
letters = str.extract_upper_case_letters
# ["H", "W"]

1 Response
Add your response

how to do the same for lower case letters and special characters?

over 1 year ago ·