Solved

Conversion question

Posted on 2011-02-28
13
232 Views
Last Modified: 2012-05-11
Hi,
I have a following cod in VB and I am trying to convert it to C# but stack on these few lines...  Thanks!

CardNoLen = Len(strCardNo)
Masked15 = "*****"
Masked16 = "******"

Select Case CardNoLen
  Case 15
    strMasked = Masked15
    strMaskedCreditCardNo = Replace(CardNo, Mid(CardNo, 7, 5), strMasked)

  Case 16
    strMasked = Masked15
    strMaskedCreditCardNo = Replace(CardNo, Mid(CardNo, 7, 6), strMasked)
End Select

0
Comment
Question by:avi7
  • 5
  • 3
  • 3
  • +1
13 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 35003309
what exactly are you trying to achieve with the replace?

there is a replace function in c# for strings, I just need to know what your objective is.

here is a link that has c# examples.
int CardNoLen = strCardNo.Length;
string Masked15 = "*****";
string Masked16 = "******";

switch(CardNoLen)
{
  Case 15:
    strMasked = Masked15;
    strMaskedCreditCardNo ="";
    break;
  Case 16:
    strMasked = Masked15
    strMaskedCreditCardNo = "";
    break;
}

Open in new window

0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 40 total points
ID: 35003317
sorry here is the link -- i made the value "" for the replace function until we can move forward... the rest should be valid

http://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.71).aspx
0
 

Author Comment

by:avi7
ID: 35003367
Here is what I am trying to do:
if card is 15 digits long it should look like: 123456*****1234 in this examp I have (5*)
if card is 16 digits long: 123456******1234 in this (6*)
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35003406
I believe it should be like in the code below.

The goal is quite obvious: to not print entire credit card number (security and print only start end end of the number. Quite common operation. But I've seen more digits to be hidden...
string strMaskedCreditCardNo = "";
            string strCardNo = "1111222233334444";
            string strMasked = "";

            int CardNoLen = strCardNo.Length;

            string Masked15 = "*****";
            string Masked16 = "******";

            switch(CardNoLen)
            {
              case 15:
                strMasked = Masked15;
                strMaskedCreditCardNo = strMaskedCreditCardNo.Replace(strMaskedCreditCardNo.Substring(6, 5), strMasked);
                break;
              case 16:
                  strMasked = Masked15;
                  strMaskedCreditCardNo = strCardNo.Replace(strCardNo.Substring(6, 6), strMasked);
                break;
            }

            MessageBox.Show(strMaskedCreditCardNo);

Open in new window

0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35003409
The output in my case:
111122******4444
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 230 total points
ID: 35003416
Here is the complete sample based on p_davis' with the string replace and substring methods.  Btw, the original VB code has under 'Case 16' 'strMasked = Masked15', but I think it should be 'strMasked = Masked16' instead.  Please verify.

string strMasked="", strMaskedCreditCardNo = "";
string strCardNo = "1234567890123456";

int CardNoLen = strCardNo.Length;
string Masked15 = "*****";
string Masked16 = "******";

switch(CardNoLen)
{
  case 15:
	strMasked = Masked15;
	strMaskedCreditCardNo = strCardNo.Replace(strCardNo.Substring(6, 5), strMasked);
	break;
	
  case 16:
	strMasked = Masked16;
	strMaskedCreditCardNo = strCardNo.Replace(strCardNo.Substring(6, 6), strMasked);
	break;
}

Console.WriteLine(strMaskedCreditCardNo);
Console.WriteLine(strMaskedCreditCardNo.Length);

Open in new window


It displays:

123456******3456
16

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 230 total points
ID: 35003418
Sorry - found eror in my code...
private void button2_Click(object sender, EventArgs e)
        {
            string strMaskedCreditCardNo = "";
            string strCardNo = "1111222233334444";
            string strMasked = "";

            int CardNoLen = strCardNo.Length;

            string Masked15 = "*****";
            string Masked16 = "******";

            switch(CardNoLen)
            {
              case 15:
                strMasked = Masked15;
                strMaskedCreditCardNo = strCardNo.Replace(strCardNo.Substring(6, 5), strMasked);
                break;
              case 16:
                  strMasked = Masked16;
                  strMaskedCreditCardNo = strCardNo.Replace(strCardNo.Substring(6, 6), strMasked);
                break;
            }

            MessageBox.Show(strMaskedCreditCardNo);

        }

Open in new window

0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35003423
Oops. Too slow.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35003434
What's slow?
0
 

Author Comment

by:avi7
ID: 35003437
Thanks to all...
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35003452
@anarki_jimbel: I meant my post. It was a couple mins after yours.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 35003527
wdosanjos, thanks to p_davis we came up with exactly same code, interesting... :)

0
 
LVL 22

Expert Comment

by:p_davis
ID: 35003544
how rare and refreshing.... thanks for the nod fellas =). have a great week.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now