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:
string[] buffer = new string[]{ "first element", "second element", };
foreach (string element in buffer)
Console.WriteLine(element);
or, for example:
string[] buffer = new string[]{ "first element", "second element", };
for (int index = 0; index < buffer.Length; ++index)
Console.WriteLine(buffer[index], 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