Reading digits from String

Hi guys,

I'm kind of new at this and I seem to have very small experience on the C# language... I hope someone can help me out.

I need to built a console application that will read a card number with the following format ( 999-999999-9). The program needs to identify the la number which the verification digit . With the digit it determine if the card number is valid or not.

I started out using a long variable but it wont accept the "-" so i decided to use a string which it can take the entire statement as the software requires. How i can read the digits ignoring the "-" and storing the last number on a seperate variable


Thanks




Answer this question

Reading digits from String

  • Santacruzshores

    So you want it effectively split by "-" Hell... that's even easier!

    string[] parts = input.Split('-');

    After that line of code, you've got an array with 3 elements (assuming the format of the input is the same as described above) and the parts[0] contains the first three digits, parts[1] the middle and parts[2] the verification number.



  • Janicita

    Guys thanks for replying

    I got the first part of the program done thank to all The next trick is to place each numer to an array for example

    0011594088

    to an array it will has be to asign this way

    array[0] =0

    array[1] =0

    array [2]=1

    array[ 3]=1

    array[4]=5 ect until it reaches the last number which is 8.

    Can this be done



  • lord_8

    string[] parts = cardNo.Split('-');

    string numPart = parts[0] + parts[1];

    string verification = parts[2];



  • .Net Queen

    Rather than try to read in the string and ignore the "-" chars... you might consider reading in the entire string and processing around them like so:

    string input = "999-999999-9";

    //locate last dash

    int pos = input.LastIndexOf("-");

    //Create string of containing everything up but not including final dash

    //And remove extra dash(es)

    string numString = input.Substring(0, pos).Replace("-", "");

    //Create string of everything after final dash

    string checkString = input.Substring(pos + 1);

    //Convert both to integers:

    int num = int.Parse(numString);

    int check = int.Parse(checkString);

    Is this what you are looking for



  • Rajesh.Y

    Brendan Grant,

    Thanks a million for replying..!!!

    The code does exactly what i want but i see it ignores the zeros when i enter it in the following format

    001-1589488-0

    so the output should be more like:

    First three = 001

    middledigits=1589488

    verification number = 0

    Can this be done

    Thanks



  • Roachy

     

    string[] parts = cardNo.Split('-');

    string numPart = parts[0] + parts[1];

    string verification = parts[2];

    string fullNum = numPart + verification

    array[0] = fullNum[0].ToString();

    array[1] = fullNum[1].ToString();

    ...

    array[ n ] = fullNum[ n ].ToString();

    you just have to cast the fullNum[index] to the array's data type.  you can also use a loop to populate your array[] variable...

    for (int i=0; i<array.Length; i++)

    {

         array[ i ] = fullNum[ i ].ToString();

    }



  • Reading digits from String