Click here to Skip to main content
15,905,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I run the code, it says that "Index was outside the bounds of the array."
C#
string xyz =";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";
string[] buffer1 = new string[]{};
string[] lines1 = xyz.Split(':');
            for (int i = 0; i < lines1.Length; i++)
            {
                //string[] lines2 = lines1[i].Split(';');
                string[] lines2 = lines1[i].Split(new char[] { ';' } ,                StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < lines2.Length; j++)
                {
                    buffer1[j] = lines2[j];
                    
                 }

                
            }


In the buffer1[j] = lines2[j], I am copying the value of each lines2 in buffer1 which I need to use after the second loop. But I am getting that exception in that line when I run the code.
Posted
Updated 6-Aug-14 9:10am
v2
Comments
Dilan Shaminda 6-Aug-14 15:02pm    
syntax errors in this line string xyz ="";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;"";
is xyz is an array?update your question please
JaideepChandra 6-Aug-14 15:11pm    
xyz is a string.. not an array
PIEBALDconsult 6-Aug-14 15:12pm    
This appears to be a follow-on to this:
http://www.codeproject.com/Questions/803944/Substring-between-two-characters?arn=0
JaideepChandra 6-Aug-14 15:14pm    
^^ yes it is.. but it is different problem where I am stuck.
[no name] 6-Aug-14 16:57pm    
So? Debug it. There is nothing at all mysterious about the error message.

Member 10472666 wrote in a comment to Solution 1:
Then how should I declare the array, buffer1.. so that I can do that assignment of values in buffer1 inside the loop?
You declaration has no problems; it's just has zero elements in the array. It's not a matter of the declaration, it's a matter of having some elements in an array, the data; it's up to you where do you get this data. For example:
C#
string[] buffer = new string[]{ "first element", "second element", };
foreach (string element in buffer)
   Console.WriteLine(element); // use somehow each element

or, for example:
C#
string[] buffer = new string[]{ "first element", "second element", };
for (int index = 0; index < buffer.Length; ++index)
   Console.WriteLine(buffer[index], index); // use somehow element and its index


If you don't know in advance how much elements you need, use one of the generic collections, such as System.Collections.Generic.List<string>. (Never use ArrayList, it was rendered obsolete as early as of .NET v.2.0; due to the need of type cast, this is much more error-prone than generic collections and has no benefits over generic collections at all).

[EDIT]

In your case, the mistake is using buffer1 at all. How to modify your code? It depends, you never explained your goal. It's likely that you don't need double-level split. If you want a flat array/list of words, you should use only one split, by ':' and ';' at the same time. If you need split by ':' and then by ';', you don't need string[] type, instead you would need jagged array string[][].

If you explain what exactly you need to achieve, I'll explain you how to do it. Also, do you need to have all those blank characters? If you want to remove it, you would need to split by new char[] {' ', ',', ';', } with the StringSplitOptions option for removing empty elements:
http://msdn.microsoft.com/en-us/library/ms131448%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
v5
Comments
Dilan Shaminda 7-Aug-14 1:10am    
Thank you for the nice explanation sir :-) I didn't noticed the fact you mentioned about ArrayList before. My +5
Sergey Alexandrovich Kryukov 7-Aug-14 1:18am    
Thank you.
—SA
JaideepChandra 7-Aug-14 15:23pm    
Thank you so much for your explanation.. It was very helpful.
Actually I need to put those values in an excel sheet where abc,def and xyz are the headings and the other values will come to according columns..Like under "abc" heading 123,234,345 will come under that column ..Similarly for others.
Sergey Alexandrovich Kryukov 7-Aug-14 16:24pm    
You are very welcome.
Will you accept the answer formally them (green "Accept" button; you always can accept more than one). In all cases, your follow-up questions will be welcome. Do you need so further help.
—SA
buffer1 is an empty array.

C#
string[] buffer1 = new string[]{};
buffer1[j] = ...
 
Share this answer
 
Comments
JaideepChandra 6-Aug-14 15:12pm    
Then how should I declare the array, buffer1.. so that I can do that assignment of values in buffer1 inside the loop?
PIEBALDconsult 6-Aug-14 15:15pm    
You can't without knowing how many you need. You might want a List instead.
But as with the question yesterday, this doesn't appear to be the best technique to achieve your goal.
Dilan Shaminda 6-Aug-14 15:21pm    
use ArrayList or Generic Lists.
Sergey Alexandrovich Kryukov 6-Aug-14 16:56pm    
No, don't ever use ArrayList...
—SA
Dilan Shaminda 6-Aug-14 21:42pm    
sir may i know the reason for that to improve my knowledge? :-)
Hello ,
Can you Use List data type.I have tried your code this works with list data type

string xyz =";abc: 123 ;234 ;345 ;def: 234 ;456 ;xyz: 123;";
                       System.Collections.Generic.List<string> listbuffer = new   System.Collections.Generic.List<string>();//Changed Here
            string[] lines1 = xyz.Split(':');

                        for (int i = 0; i < lines1.Length-1; i++)
                        {
                            //string[] lines2 = lines1[i].Split(';');
                            string[] lines2 = lines1[i].Split(new char[] { ';'},StringSplitOptions.RemoveEmptyEntries);
                           // buffer1 = new string[] { };
                            for (int j = 0; j < lines2.Length-1; j++)
                            {
                                //buffer1[j] = lines2[j];
                               listbuffer .Add (lines2[j].ToString());
                             }
                        }


After this chaged it to
string[] buffer1= listbuffer.ToArray();


Try This . Let me know whether this worked for you or not.
Happy Coding :-)
 
Share this answer
 
v4
Assuming that the provided string is actually a list of key/value pairs where each value may in turn be a list of values, I suggest you produce a Dictionary of these Lists.
Also, it appears that the data is regular enough that you can use a Regular Expression to extract the keys and values.
Have a look at the following and see whether or not it does what you need.

C#
namespace X
{
  using ValueList = System.Collections.Generic.List<string> ;

  public static class Y
  {
    private static readonly System.Text.RegularExpressions.Regex r =
      new System.Text.RegularExpressions.Regex ( @"(;(?'key'\w+):)|(?'val'\w+)" ) ;

    public static System.Collections.Generic.Dictionary<string,ValueList>
    ParseKeyValueDictionary
    (
      string Data
    )
    {
      System.Collections.Generic.Dictionary<string,ValueList> result =
        new System.Collections.Generic.Dictionary<string,ValueList>() ;

      System.Text.RegularExpressions.MatchCollection mat = r.Matches ( Data ) ;

      string key = System.String.Empty ;

      foreach ( System.Text.RegularExpressions.Match m in mat )
      {
        if ( m.Groups [ "key" ].Value.Length > 0 )
        {
          result [ key = m.Groups [ "key" ].Value ] = new ValueList() ;
        }
        else
        {
          result [ key ].Add ( m.Groups [ "val" ].Value ) ;
        }
      }

      return ( result ) ;
    }
  }
}
 
Share this answer
 

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