Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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

Here is my code.

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();
    }
}

It is not very memory efficient. Can you please suggest a better way or improvement.

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 4 hours ago
up vote 29 down vote accepted

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.

share|improve this answer
2  
URLEncoder in particular, encodes space as + which may be undesireable in some cases. – nitro2k01 9 hours ago
5  
"Don't do it yourself" is a golden rule of coding in mature languages that I took far too long to learn.... – Toadfish 8 hours ago
1  
URLEncoder is for form encoding. URL escaping is done with java.net.URI. – VGR 6 hours ago

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
{

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

}
share|improve this answer
1  
While good advice in general, it has nothing to do with the question as posted. – fluffy 7 mins ago

Your Answer

 
discard

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.