Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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) {
        // 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();
share|improve this question
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: More likely, they first pressed the {} button, and then pasted in their code. – Ilmari Karonen Feb 20 at 21:29
@CaptainMan.. you are so correct. I would take care from next time. – Mosbius8 Feb 21 at 12:46
up vote 52 down vote accepted

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

share|improve this answer
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 – 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.

share|improve this answer

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

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

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.