Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Conversion question

Posted on 2011-02-28
13
Medium Priority
?
242 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 160 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 920 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 920 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview

610 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