Solved

Conversion question

Posted on 2011-02-28
13
231 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The output in my case:
111122******4444
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 230 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 230 total points
Comment Utility
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
Comment Utility
Oops. Too slow.
0
 
LVL 29

Expert Comment

by:anarki_jimbel
Comment Utility
What's slow?
0
 

Author Comment

by:avi7
Comment Utility
Thanks to all...
0
 
LVL 23

Expert Comment

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

Expert Comment

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

0
 
LVL 22

Expert Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

10 Experts available now in Live!

Get 1:1 Help Now