Click to See Complete Forum and Search --> : find/replace question


Splatt
October 5th, 2000, 01:42 PM
Is there any way to do a find/replace of carraige returns with linefeeds in VJ++ 6.0 Pro sp4?

"There's nothing more dangerous than a resourceful idiot." ---Dilbert

Gremlin
October 7th, 2000, 04:55 AM
try this,


try
{

buffer = new StringBuffer((int) toLoad.length());
in = new InputStreamReader(new FileInputStream(toLoad));

char[] buf = new char[BUFFER_SIZE];
int len;
int lineCount = 0;
boolean CRLF = false;
boolean CROnly = false;
boolean lastWasCR = false;

// we read the file till its end (amazing, hu ?)
while ((len = in.read(buf, 0, buf.length)) != -1)
{
int lastLine = 0;
for (int i = 0; i < len; i++)
{
// ++count;
switch(buf[i])
{
// and we convert system's carriage return char into \n
case '\r':
if (lastWasCR)
{
CROnly = true;
CRLF = false;
} else
lastWasCR = true;
// if \r delete and replace with \n
buffer.append(buf, lastLine, i - lastLine);
buffer.append('\n');
lastLine = i + 1;

break;
case '\n':
if (lastWasCR)
{
CROnly = false;
CRLF = true;
lastWasCR = false;
lastLine = i + 1;
} else {
CROnly = false;
CRLF = false;
buffer.append(buf, lastLine, i - lastLine);
buffer.append('\n');
lastLine = i + 1;
}
break;
default:
if (lastWasCR)
{
CROnly = true;
CRLF = false;
lastWasCR = false;
}
break;
}
}
}
in.close();
in = null;

if (buffer.length() != 0 && buffer.charAt(buffer.length() - 1) == '\n')
buffer.setLength(buffer.length() - 1);