aspose file tools*
The moose likes Beginning Java and the fly likes mask cc number in log files from a string in the log file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "mask cc number in log files from a string in the log file" Watch "mask cc number in log files from a string in the log file" New topic
Author

mask cc number in log files from a string in the log file

lina ajay
Ranch Hand

Joined: Apr 04, 2008
Posts: 31
hi

issue :--

i want to mask the creditcard number which gets displayed in my log files .
i have a class for creditcard processing which gets the information abt creditcard and writes it to a log file - i have to find the position of the cc in the long string" transaction string" which has details abt user id etc too.
this is what i have done so far

int logtranString = transactionString.indexOf("cc");

can anyone please help me in accomplishing the rest of it.

thanks a lot
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

This may be a good place to use RegEx with the String.replaceAll(regex, replacement) method.

Look into the Pattern API and see if you can't find a Pattern that would match:
"4 digits" dash "4 digits" dash "4 digits" dash "4 digits" coming after "cc"

If you aren't familiar with RegEx then your strategy may be:
  • Use a StringBuilder:
  • Copy all the contents of the String before the indexOf("cc") [perhaps + 2 so "cc" appears in the output] into the StringBuilder
  • Append the mask for the credit card number onto the StringBiolder
  • Append all the contents of the String after indexOf("cc") + 2 + length of the credit card number into the StringBuilder
  • Turn the StringBuilder into a String.


  • The API for String and StringBuilder should help.


    Steve
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19697
        
      20

    That works for most CC numbers, but 16 is not a hard limit. In fact, actual lengths range from 12 to 19.

    But if you limit input to 16 characters, than the following regex should do:

    In other words:
    - 4 characters
    - a space, dash or nothing followed by 4 characters, and this entire part times three


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38902
        
      23
    By four characters, Rob, you presumably mean four digits?
    lina ajay
    Ranch Hand

    Joined: Apr 04, 2008
    Posts: 31
    when i use StringBuilder SB= new String Builder(); I am getting an error message that -- string builder not defined. i have imported java.util.*; is there any other class to be imported to use this.
    jvm on my machine is 1.5 version.

    thanks
    Steve Luke wrote:This may be a good place to use RegEx with the String.replaceAll(regex, replacement) method.

    Look into the Pattern API and see if you can't find a Pattern that would match:
    "4 digits" dash "4 digits" dash "4 digits" dash "4 digits" coming after "cc"

    If you aren't familiar with RegEx then your strategy may be:
  • Use a StringBuilder:
  • Copy all the contents of the String before the indexOf("cc") [perhaps + 2 so "cc" appears in the output] into the StringBuilder
  • Append the mask for the credit card number onto the StringBiolder
  • Append all the contents of the String after indexOf("cc") + 2 + length of the credit card number into the StringBuilder
  • Turn the StringBuilder into a String.


  • The API for String and StringBuilder should help.
    fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11327
        
      16

    you want "StringBuilder", not "String Builder".

    no space.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    lina ajay
    Ranch Hand

    Joined: Apr 04, 2008
    Posts: 31
    i tried either way. it still shows an error message??

    fred rosenberger wrote:you want "StringBuilder", not "String Builder".

    no space.
    Steve Luke
    Bartender

    Joined: Jan 28, 2003
    Posts: 4181
        
      21

    lina ajay wrote:i tried either way. it still shows an error message??

    fred rosenberger wrote:you want "StringBuilder", not "String Builder".

    no space.


    Then verify that you have Java 1.5+ installed, and that you aren't compiling the code to be 1.4 compatible.

    StringBuilder is available in Java 1.5 and above. If you want to be backwards compatible before J1.5, use StringBuffer instead.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19697
        
      20

    Campbell Ritchie wrote:By four characters, Rob, you presumably mean four digits?

    I surely do.
     
    jQuery in Action, 2nd edition
     
    subject: mask cc number in log files from a string in the log file