• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Sheriff
    Posts: 22656
    126
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Marshal
    Posts: 75857
    361
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    By four characters, Rob, you presumably mean four digits?
     
    lina ajay
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    lowercase baba
    Posts: 13056
    67
    Chrome Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    you want "StringBuilder", not "String Builder".

    no space.
     
    lina ajay
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i tried either way. it still shows an error message??

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

    no space.

     
    Steve Luke
    Bartender
    Posts: 4179
    22
    IntelliJ IDE Python Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 22656
    126
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

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


    I surely do.
     
    pie. tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic