Somethin strange about ^

I'm trying to eliminate all the ^ in a string by using the replaceAll method.

This is the code...

class Test

{

public static void main(String[] args)

{

String string = "^";

System.out.println(string);

string = string.replaceAll("^","here's one!!");

System.out.println(string);

}

}

The above code compiles fine but when I run it, I dunno... I got a very strange output!! The output I got is-

C:\workarea>javac Test.java

C:\workarea>java Test

^

here's one!!^

^ isn't removed but the replace string (here's one!!) is COMING!!

I tried it with ~(instead of ^) but that's workin.. Does anybody know what's goin on?

[737 byte] By [jozzyjacz] at [2007-11-26 9:39:13]
# 1

First of all this is the wrong forum for your post. This forum is for the Sun Web Proxy Server forum. That being said the answer to your question is that the "^" in your replaceAll call refers to a regular expressions and ^ has 2 meanings. When its used by itself it refers to the beginning of a line. When its used in selector braces (i.e. [^abc} it means everything except abc. In order for you to replaceAll all literal occurences of "^" you will need to escape its regex meaning. Try this instead (use "\v").

string = string.replaceAll("\^","here's one!!");

good luck

dlochart at 2007-7-7 0:35:03 > top of Java-index,Web & Directory Servers,Web Servers...