Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

hide credit card numbers (See related posts)

// hide all digits of a credit card number except the last 4. eg<br/>
// cc = "12345678901123456"
// hide_card_number(cc)
// [ "************3456" ]

  def hide_card_number(card_number)
    size = card_number.to_s.size - 1
    cc = "" 
    i = 0
    0.upto(size) do 
      if( i > size - 4 )
      else
        cc += "*" 
      end
      i += 1
    end
    cc = cc + card_number[size-3, size].to_s
    return cc 
  end

Comments on this post

toolmantim posts on May 16, 2007 at 21:54
or:

def hide_card_number(card_number)
  card_number.sub((part = card_number[0..-5]), '*' * part.length)
end
ChronicStar posts on May 16, 2007 at 23:13
or even:

def hide_card_number(card_number,len=4,char='*')
  card_number.gsub(/^(.*)(.{#{len}})$/){char*$1.size + $2}
end


Okay, it's kind of similar to the previous one.

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts