Solved

Conversion question

Posted on 2011-02-28
13
238 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
Independent Software Vendors: 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 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