Java String.replaceAll() not behaving as expected  
Author Message
wkrick





PostPosted: 2004-8-5 5:36:00 Top

java-programmer, Java String.replaceAll() not behaving as expected Given a string "s", I'd like to convert all the line separators into
their "escaped" string equivalents. i.e. a dos/windows newline gets
converted into the literal string "\r\n".

I tried this in my java code...

s = s.replaceAll("\n", "\\n");
s = s.replaceAll("\r", "\\r");

but I end up with "rn" instead of "\r\n"

I assume the problem has something to do with the regex pattern
replacement but it seems like it should work.

What am I doing wrong?

...
Krick
 
David Hilsee





PostPosted: 2004-8-5 9:01:00 Top

java-programmer >> Java String.replaceAll() not behaving as expected "William Krick" <email***@***.com> wrote in message
news:email***@***.com...
> Given a string "s", I'd like to convert all the line separators into
> their "escaped" string equivalents. i.e. a dos/windows newline gets
> converted into the literal string "\r\n".
>
> I tried this in my java code...
>
> s = s.replaceAll("\n", "\\n");
> s = s.replaceAll("\r", "\\r");
>
> but I end up with "rn" instead of "\r\n"
>
> I assume the problem has something to do with the regex pattern
> replacement but it seems like it should work.
>
> What am I doing wrong?

I think you want \\\\n and \\\\r instead of \\n and \\r. That's 4 slashes:
2 for the compiler and 2 for the regex engine.

--
David Hilsee


 
wkrick





PostPosted: 2004-8-6 0:39:00 Top

java-programmer >> Java String.replaceAll() not behaving as expected "David Hilsee" <email***@***.com> wrote in message news:<email***@***.com>...
> "William Krick" <email***@***.com> wrote in message
> news:email***@***.com...
> > I tried this in my java code...
> > s = s.replaceAll("\n", "\\n");
> > s = s.replaceAll("\r", "\\r");
> > but I end up with "rn" instead of "\r\n"
> > What am I doing wrong?
>
> I think you want \\\\n and \\\\r instead of \\n and \\r. That's 4 slashes:
> 2 for the compiler and 2 for the regex engine.

Thanks. That did the trick.

It's times like this that I wish that java had two versions of replace
all. One that used regular expression pattern matching and one that
was just a dumb direct string replacement.
 
 
David Hilsee





PostPosted: 2004-8-6 6:54:00 Top

java-programmer >> Java String.replaceAll() not behaving as expected "William Krick" <email***@***.com> wrote in message
news:email***@***.com...
> "David Hilsee" <email***@***.com> wrote in message
news:<email***@***.com>...
> > "William Krick" <email***@***.com> wrote in message
> > news:email***@***.com...
> > > I tried this in my java code...
> > > s = s.replaceAll("\n", "\\n");
> > > s = s.replaceAll("\r", "\\r");
> > > but I end up with "rn" instead of "\r\n"
> > > What am I doing wrong?
> >
> > I think you want \\\\n and \\\\r instead of \\n and \\r. That's 4
slashes:
> > 2 for the compiler and 2 for the regex engine.
>
> Thanks. That did the trick.
>
> It's times like this that I wish that java had two versions of replace
> all. One that used regular expression pattern matching and one that
> was just a dumb direct string replacement.

Someone else asked for the same thing on the first of this month. Here's
what I said to them:

Simple string substitution can be performed using indexOf() and some other
methods/classes. Here's an example utility method that can replace all
instances of a substring (toReplace) inside a given string (source) with a
replacement string (replacement).

public static String replaceAll(
String source,
String toReplace,
String replacement
) {
int idx = source.lastIndexOf( toReplace );
if ( idx != -1 ) {
StringBuffer ret = new StringBuffer( source );
ret.replace( idx, idx+toReplace.length(), replacement );
while( (idx=source.lastIndexOf(toReplace, idx-1)) != -1 ) {
ret.replace( idx, idx+toReplace.length(), replacement );
}
source = ret.toString();
}

return source;
}


--
David Hilsee