B
Benjamin H. Brinckerhoff
All,
Maybe someone can clear some confusion I am having concerning
replaceAll. Here's my code:
String str = "a\nb";
String str2 = str.replaceAll("\n","\\\\n");
String str3 = str.replaceAll("\n","\\n");
System.out.println(str);
System.out.println(str2);
System.out.println(str3);
This prints out the following when run
a
b
a\nb
anb
The first two printlns print out exactly what I would expect. But the
third one, using str3, confuses me. Here's what I thought was going
on:
str, seen as an array of chars looks like (I'm using [] to show
different elements in array)
[a][\n]
replaceAll should find all characters that are \n , and replace them
with \\n, i.e. the "\" character and the "n" character, so I would
think str3 would be
[a][\][n]
Why does this end up printing out as "anb"? I mean, "\" isn't even an
ASCII character. Yeah, I'm confusing myself, if anyone out there has a
clear explanation, I'd love to hear it. Thanks.
Ben
Maybe someone can clear some confusion I am having concerning
replaceAll. Here's my code:
String str = "a\nb";
String str2 = str.replaceAll("\n","\\\\n");
String str3 = str.replaceAll("\n","\\n");
System.out.println(str);
System.out.println(str2);
System.out.println(str3);
This prints out the following when run
a
b
a\nb
anb
The first two printlns print out exactly what I would expect. But the
third one, using str3, confuses me. Here's what I thought was going
on:
str, seen as an array of chars looks like (I'm using [] to show
different elements in array)
[a][\n]
replaceAll should find all characters that are \n , and replace them
with \\n, i.e. the "\" character and the "n" character, so I would
think str3 would be
[a][\][n]
Why does this end up printing out as "anb"? I mean, "\" isn't even an
ASCII character. Yeah, I'm confusing myself, if anyone out there has a
clear explanation, I'd love to hear it. Thanks.
Ben