Click to See Complete Forum and Search --> : problem with this newline


stevendo
August 29th, 2008, 07:54 PM
i have this code


//Array of each function
ArrayList mylist = new ArrayList();
ArrayList mycommands = new ArrayList();

//Divide the file into lines
string[] tokens = myfile.Split(Environment.NewLine.ToCharArray());
foreach (string item in tokens)
mylist.Add(item);

foreach (string command in mylist)
Console.WriteLine(command);

//Divide the lines into commands
foreach (string item in mylist)
{
string[] cmdlst = item.Split(new char[] {';'});
foreach (string command in cmdlst)
{
string newstring = command;
newstring = newstring.Trim();
newstring = newstring.Replace("\r\n", "");
newstring = newstring.Replace("\r", "");
newstring = newstring.Replace("\n", "");
mycommands.Add(newstring);
}

foreach (string command in mycommands)
Console.WriteLine(command);


ok so when it prints out the second time i get a new line for every time i do WriteLine () and then i get an extra new line and i get double spaces in the console.

no matter how hard i try the second time around i cannot get rid of the pesky new lines

any ideas?

.Net framework 3.5

Arjay
August 29th, 2008, 08:52 PM
When you step through the code in a debugger what does the string look like when you add it to the list (see in red below)?


foreach (string command in cmdlst)
{
string newstring = command;
newstring = newstring.Trim();
newstring = newstring.Replace("\r\n", "");
newstring = newstring.Replace("\r", "");
newstring = newstring.Replace("\n", "");
mycommands.Add(newstring);
}

stevendo
August 29th, 2008, 09:19 PM
in response to your quick reply :) it looks fine and has now new lines in it. when i use


foreach (string command in mycommands)
Console.Write(command);


it all displays in a row with no new lines
but when i do


foreach (string command in mycommands)
Console.Write(command + "\n");


i get huge gaps asife there is more than one newline

weird i think there may be entrys with nothing "" you know what i mean empty slots for some reason

i will continue to look further

Arjay
August 29th, 2008, 11:07 PM
I don't use ArrayLists anymore since generics came on the scene in .Net 2.0.

The equivalent generic would be
List< string > mycommands = new List< string >( );

I doubt it's your problem, but.....