lja7ra
Last Updated: February 25, 2016
·
3.362K
· dennismonsewicz
392321 692815809906 50504925 33878144 117300072 n

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"]
Say Thanks
Respond

1 Response
Add your response

18558
Vk1w58kl normal

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

over 1 year ago ·