[dba-VB] C# replace a special character

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Tue Oct 20 14:49:26 CDT 2009


Hi John,

Try this code snippet changing input and output files' paths:

int bufferSize = 1024*1024;

string inputFile = @"c:\temp\inputFile.txt";
string outputFile = @"c:\temp\outputFile.txt";

System.IO.FileStream inputStream =
    new System.IO.FileStream(
        inputFile,
        System.IO.FileMode.Open,
        System.IO.FileAccess.Read,
        System.IO.FileShare.None,
        bufferSize);
System.IO.FileStream outputStream =
    new System.IO.FileStream(
        outputFile,
        System.IO.FileMode.Create,
        System.IO.FileAccess.Write,
        System.IO.FileShare.None,
        bufferSize);

byte[]data = new byte[bufferSize];
int offset = 0;
int totalLength = 0;
int length = 0;
while ((length = inputStream.Read(data, offset, bufferSize)) > 0)
{
    totalLength += length;

    string text = System.Text.Encoding.ASCII.GetString(data);
    text = text.Replace((char)26, ' ').Replace((char)0x1a,' ') ;
    data = System.Text.Encoding.ASCII.GetBytes(text);    
    
    outputStream.Write(data, offset, length);
}

inputStream.Close();
outputStream.Close();

Console.WriteLine("Total length test: {0}", totalLength);

HTH.

--
Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Tuesday, October 20, 2009 7:45 PM
To: VBA
Subject: [dba-VB] C# replace a special character

I have a file which contains a sprinkling of a special character - decimal
26, hex 1A (the SUB 
character).  This special character in the CSV file output to Accuzip causes
Accuzip to hiccup.  In 
order to get on with my life I need to pre-process the files to get rid of
this special character. 
Because of the size of these files (hundreds of mbytes) I need to do a
readline / replace / 
writeline kind of thing.  I think I need to use the stringbuilder.replace
but I do not know how to 
specify replacing this hex 1A with something else.  The something else would
ideally be nothing 
(empty string) but can be any valid alpha character (A, B etc).

Can anyone help me with this replace part of the problem.  C# syntax please.

-- 
John W. Colby
www.ColbyConsulting.com
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4527 (20091020) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru


 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4527 (20091020) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list