hello friends
I am new to c# and i want to know how to read contents from a file and store in an array not word by word but depending on the no of characters including space
example:
string:Hi how are you?
packet size:4
then i shall get: a[0]=hi h
a[1]=ow a
a[2]=re y
a[3]=ou?
please help
thanks
theLizardPosted Mar 3, 2016, 9:56 PM
string[] words = new string[0];
int packetSize = 4;
while(s.Length > 0)
{
Array.Resize(ref words, words.Length + 1);
if (s.Length > packetSize)
words[words.Length - 1] = s.Substring(0, packetSize);
else
{
words[words.Length - 1] = s;
s = "";
}
if (s.Length > packetSize)
s = s.Substring(packetSize);
ali tuncerPosted Mar 3, 2016, 9:13 PM
Easier to store it in a list, array is not resizable..If you want you can modify it to store in an array..
int packetSize = 4; lst = new List();
string test = File.ReadAllText(@"Path");
List
while (test.Length > packetSize)
{
lst.Add(test.Substring(0, packetSize));
}
if (test != string.Empty)
{
lst.Add(test);
}
Amit Kumar SinghPosted Mar 3, 2016, 9:01 PM
{}}2.