Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Image

I have tried this code but error "index out of range" at cbay.ABS = columnsC[5] because the second line return only 4 instead of 6 elements like in 1st line. I want the 2nd line also return 6 elements.

C#
using (StringReader strrdr = new StringReader(strData))
{
    string str;
    while ((str = strrdr.ReadLine()) != null)
    {
        //  str = str.Trim();
        if ((Regex.IsMatch(str.Substring(0, 1), @"J")) || (Regex.IsMatch(str.Substring(0, 1), @"C")))
        {
            columnsC = Regex.Split(str, " +");
            cbay.AC = columnsC[1];
            cbay.AU = columnsC[2];
            cbay.SA = columnsC[3];
            cbay.ABS = columnsC[5];
            // cbay.ABS = str;
        }
    }
}
Posted
Updated 29-Mar-13 0:37am
v4
Comments
Menon Santosh 29-Mar-13 6:38am    
please post your string which you want to split and are you reading the string from text file
khoirom 29-Mar-13 6:40am    
Yes, it in the "Image" link

Firstly, stop using regexes - they are a sledghammer to crack a very simple nut.
For example, this:
C#
if ((Regex.IsMatch(str.Substring(0, 1), @"J")) || (Regex.IsMatch(str.Substring(0, 1), @"C")))
Can be rewritten as this:
C#
if (str[0] == 'J' || str[0] == 'C')
which is a heck of a lot easier to read!

Secondly, splitting your data is probably not what you want to do: since it is organised in strict columns, I would manuaglly extract the header line, then use substring to extract the data elements: the fact that some elements have only 3 sets of numbers is important, becasue teh column in which the number occurs tells you which part of the data it is: in teh case of your "C" line, the numbers are the AU, SA, and BSS values, not the AC, AU and SA values you would have to assume from the data if you do a crude split operation as you are doing at present.

Use string.SubString in combination with string.Trim to break up your line, then use int.TryParse to give you actual values instead of assuming character values.
 
Share this answer
 
thank you very much. I had rewriten like this:

C#
using (StringReader strrdr = new StringReader(strData))
                       {
                           string str;
                           while ((str = strrdr.ReadLine()) != null)
                           {
                               str = str.TrimStart();

                               if (str.Length > 20 && (str[0] == 'C' || str[0] == 'I'))
                               {

                                   cbay.AC = str.Substring(1, 8).Trim();
                                   cbay.AU = str.Substring(9, 5).Trim();
                                   cbay.SA = str.Substring(14,5).Trim();
                                   cbay.ABS = str.Substring(24,5).Trim();


                               }

                               if (str.Length > 20 && str[0] == 'Y')
                               {

                                   cbay.AC1 = str.Substring(1, 8).Trim();
                                   cbay.AU1 = str.Substring(9, 5).Trim();
                                   cbay.SA1 = str.Substring(14, 5).Trim();
                                   cbay.ABS1 = str.Substring(24,5).Trim();

                                  //  'Y' always come after 'C' and 'I',  so break
                                   break;
                               }

                           }


                       }
                       //return;
                       if (String.IsNullOrEmpty(cbay.AC))  cbay.AC  = "0";
                       if (String.IsNullOrEmpty(cbay.AU))  cbay.AU  = "0";
                       if (string.IsNullOrEmpty(cbay.SA))  cbay.SA  = "0";
                       if (String.IsNullOrEmpty(cbay.ABS)) cbay.ABS = "0";

                       if (String.IsNullOrEmpty(cbay.AC1))  cbay.AC1  = "0";
                       if (String.IsNullOrEmpty(cbay.AU1))  cbay.AU1  = "0";
                       if (string.IsNullOrEmpty(cbay.SA1))  cbay.SA1  = "0";
                       if (String.IsNullOrEmpty(cbay.ABS1)) cbay.ABS1 = "0";

                       cbay.totalAU = ( int.Parse(cbay.ABS) + int.Parse(cbay.ABS1)).ToString();
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900