Solved

Conversion question

Posted on 2011-02-28
13
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

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 30

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
 
LVL 30

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 30

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 30

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

735 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