1,105,169 Community Members

String.replace() Method

Member Avatar
mr.sweetchuck
Light Poster
27 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

Try replaceAll() instead.

Member Avatar
mr.sweetchuck
Light Poster
27 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Cerberus
Junior Poster
162 posts since Sep 2006
Reputation Points: 17 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

It works for me.

Is url a String?

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
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();
		}
	}
}
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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(' ', '_');

Member Avatar
mr.sweetchuck
Light Poster
27 posts since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
1
 

Next time please let us know that you using different API

Member Avatar
ClickerA
Newbie Poster
1 post since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

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

P.S.: sorry my english

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
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.

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
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.

prim34ev
Newbie Poster
1 post since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
-1
 

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

Member Avatar
aruaditya
Newbie Poster
1 post since Mar 2014
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-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.

Member Avatar
stultuske
Posting Expert
5,306 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
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