Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Acronym Builder

DZone's Guide to

Acronym Builder

·
Free Resource
// Generates an acronym from a string. For example "Hello World Ruby SCrIpT" goes to "HWRS".


    acronym = ''
    bytes = [] # Byte array
    long_string = ' ' + long_string # so the first letter, if capital, is included
    long_string.each_byte {|byte| bytes.push(byte)} # Build array of bytes
    bytes.each_with_index do |byte, index|
      if byte > 64 and byte < 96 # This is the range for ASCII capital letters, your encoding may vary
        # Capital letter
        if bytes[index-1] == 32 # We check behind to see if the letter is preceded by a space. Again, encoding may vary. 32 is ASCII space.
          acronym = acronym + long_string[index,1]
        end
      end
    end
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}