I have pinned down the problem to a nasty string I get by reading a file (full code below).
File s.tab contains these 18 hex bytes: FF FE 41 00 0D 0A 00 0D 0A 00 0D 0A 00 42 00
Here is the debug output from my program:
b.Length=8 loop n=1, i=3, b=A??
?? B
stuck at i=3, b(i)=10 2573 3328...
done n=1, i=3, b=A??
?? B
So it is something to do with invalid unicode. I have printed out the decimal values of the characters of string b, starting at i = 3 = IndexOf("\n\n"). IndexOf seems to see the 10 as a newline (OK), and then 2573 (which is 0D 0A) as another (not OK?). Then Replace doesn't agree.
Clearly there is something wrong with the data in the file. But I still don't think this should happen. IndexOf and Replace ought to agree.
I am implementing msaeed's solution. Many thanks.
Debug code:
{
System.IO.StreamReader aFile = System.IO.File.OpenText( @"c:\xfer\s.tab");
string a = aFile.ReadToEnd();
aFile.Close();
int nn=0, ii;
Console.WriteLine ("a.Length={0}", a.Length);
while ( (ii=a.IndexOf("\n\n")) >= 0 )
{
nn++;
Console.WriteLine("loop n={0}, i={1}, a={2}"
, nn
, ii
, a);
if (ii == a.IndexOf("\n\n"))
{
Console.WriteLine ("stuck at i={0}, a(i)={1} {2} {3}..."
, ii
, (int)(a.ToCharArray()[ii])
, (int)(a.ToCharArray()[ii+1])
, (int)(a.ToCharArray()[ii+2])
);
break;
}
a = a.Replace ("\n\n", "\n");
}
Console.WriteLine("done n={0}, i={1}, a={2}", nn, ii, a);
}