1.11M Members

String.replace() Method

 
0
 

Hi, I'm trying to use the replace() method to take out all instances of " "
, (space) with an underscore, ("_"). I'm doing this because I'm using URLs to connect to a servlet. I keep getting this error:

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
        url.replace(" ", "_");

This is how I wrote my code:

url = url.replace(" ", "_");

What am I doing wrong? Any suggestions would be most welcome.

Thanks,
chuck

 
0
 

Try replaceAll() instead.

 
0
 

I'm getting this error now:

cannot find symbol
symbol  : method replaceAll(java.lang.String,java.lang.String)
location: class java.lang.String
        url = url.replaceAll(" ", "_");

That's just confusing. :confused: Any other suggestions?

 
0
 

It works for me.

Is url a String?

 
0
 

I think you forgot to change URL to string, so you can't apply String method replaceAll() to url
This can help you

import java.net.URL;
import java.net.MalformedURLException;

public class ReplaceChar
{
	public static void main(String[] args)
	{
		try
		{			
			URL url = new URL("http://www.peter-peter.pe");
			url = new URL(url.toString().replaceAll("e", "i"));
			System.out.println(url.toString());
		}
		catch(MalformedURLException mue)
		{
			mue.printStackTrace();
		}
	}
}
 
0
 

Hi, I'm trying to use the replace() method to take out all instances of " "
, (space) with an underscore, ("_"). I'm doing this because I'm using URLs to connect to a servlet. I keep getting this error:

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)

url.replace(" ", "_");

This is how I wrote my code:

url = url.replace(" ", "_");

What am I doing wrong? Any suggestions would be most welcome.

Thanks,
chuck

Do you notice something about the highlighted protions of the error message?

change url.replace(" ", "_"); to url.replace(' ', '_');

 
0
 

I have it. replaceAll is not in the Java ME api. I made a rather stupid assumption :mad: and wasted your time. I've fixed the problem . Many thanks for your time and efford. ;)
chuck

Question Answered as of 7 Years Ago by Cerberus, peter_budo and masijade
 
1
 

Next time please let us know that you using different API

 
0
 

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

 
0
 

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

No it doesn't, and besides, as of Java 5 the way he tried it in the OP will work, but since this thread is a four year dead zombie, that doesn't matter anymore, and neither does the post I'm responding to. Killing this zombie now.

 
1
 

method String.replace required char[] data type in arguments. Try:

url.replace(" ".toByteArray(), "".toCharArray());

P.S.: sorry my english

Bad advice exactly showing that you did not read whole thread otherwise you would have seen that original poster then said that Java Microedition API was in use. Unlike J2SE JME has limited classes and that also mean less methods to use.

 
-1
 

the replace method cannot be applied to Strings, only to chars

 
-1
 

String s="MXLXYXLXM";
String p=s.replace(3,'X','A');
In compiling it shows an error that method(int,char,char) not found.please anyone can explain me this I am really troubled.

 
0
 

what you could have done is start your own thread, since this is:
a. not the same issue
b. a 6 year old dead thread, that shouldn't have been revived.

what on earth is that int doing in that method call?
anyway, check this to find out what options you have for that method.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article