Java String.replaceAll() not behaving as expected

W

William Krick

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
 
D

David Hilsee

William Krick said:
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.
 
W

William Krick

David Hilsee said:
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.
 
D

David Hilsee

William Krick said:
"David Hilsee" <[email protected]> wrote in message

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;
}
 
Joined
Dec 18, 2008
Messages
1
Reaction score
0
replaceAll("\r\n", "\\\\r\\\\n") not working on Sunsolaris

Hi,

We are using the Solaris server and the solution you provided
replaceAll("\r\n", "\\\\r\\\\n") is not working.

Kindly guide me for the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top