Using the replace method?
I am trying to write a program that takes the word Mississippi and replaces all the i's with !'s and all s's with $'s. It doesnt allow me to compile the code because it says something about a syntax error on token "!". Here is the code so far:
Code :
public class Replace
{
public static void main(String[] args)
{
String word = "Mississippi" ;
String word2 = word.replace(i, !) ;
String word3 = word2.replace(s, $) ;
System.out.println(word3);
}
}
Could anyone help?
Re: Using the replace method?
Have you tried reading the Javadoc? There's an example which might help you figure out why you're getting the compile error.
edit:
gah, embarrassing error. I really need to keep my methods straight :(
Re: Using the replace method?
Quote:
Originally Posted by
helloworld922
Have you tried reading the
Javadoc... (replace doesn't replace all instances, it only replaces the first instance it finds)...
Ummm...
From String (Java Platform SE 6) and from the Java 7 reference whose link you posted:
String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar
.
Cheers!
Z
Re: Using the replace method?
Quote:
Originally Posted by
Zaphod_b
::blush:: absolutely right.
Re: Using the replace method?
Quote:
Originally Posted by
Zaphod_b
Can't believe I forgot the quotation marks -_- . Thanks guys!