# Replace space with %20

This is not very memory-efficient. Can you please suggest a better way or improvement?

public class Test{

public static void main(String[] args) {
System.out.println(replaceSpace("All men must die"));
}

public static String replaceSpace(String s) {
String[] stringArray = s.split(" ");
StringBuffer sb = new StringBuffer();
for(String s3 : stringArray) {
sb.append(s3);
sb.append("%20");
}
// if the last character is not space then, don't append %20.
if(s.charAt(s.length()-1) != ' ') {
return sb.substring(0, sb.length()-3).toString();
}

return sb.toString();
}
}

-
This is a review of your formatting. It looks like you copied and pasted your code and then put 4 spaces before public class Test{, I can tell because everywhere is properly aligned except here and after your code I see a trailing "}". In the future, you can highlight all of your code and push the button that looks like { } on the question editor and then it will put 4 spaces in front of each line so it properly formats all of your code in an easy manner. :) – Captain Man Feb 18 at 17:09
@CaptainMan.. you are so correct. I would take care from next time. – Mosbius8 Feb 21 at 12:46

Don't do this yourself. Instead use java.net.URLEncoder or another library implementation of url encoding. You'll also get support for other characters as well besides space.

-
URLEncoder in particular, encodes space as + which may be undesireable in some cases. – nitro2k01 Feb 18 at 11:56
"Don't do it yourself" is a golden rule of coding in mature languages that I took far too long to learn.... – Toadfish Feb 18 at 12:56
URLEncoder is for form encoding. URL escaping is done with java.net.URI. – VGR Feb 18 at 15:11
@VGR Amazing that this got so many upvotes being wrong... – jpmc26 Feb 19 at 20:15

Have you tried myString.replaceAll(" ", "%20")? This method is available since Java 1.4.

BTW, a StringBuilder is recommended over a StringBuffer if you do not require the synchronization offered by the latter. This class is available since Java 1.5.

edit: myString.replace(" ", "%20") also exists, this should be preferred for literal replacements and is available since Java 1.5.

-

Everyone has commented about how to better replace the spaces, I will answer how to better test.

I suggest making a proper unit test with Junit. This way you do not need to manually look at your output, you can simply run the test and it will tell you if it failed or passed. Unit testing is extremely crucial in a professional environment and is a very important skill to pick up early.

(The below assumes your class is named MyClass.)

import org.junit.Assert;
import org.junit.Test;

public class MyClassTest
{

@Test
public void testSpaceReplacement()
{
Assert.assertEquals("All%20men%20must%20die", MyClass.replaceSpace("All men must die"));
}

}

-
While good advice in general, it has nothing to do with the question as posted. – fluffy Feb 18 at 21:12
@fluffy I think this answer could be interpreted to mean "Don't name your class Test if it isn't a proper unit test". – 200_success Feb 18 at 21:40