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();
  • 1
    \$\begingroup\$ 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. :) \$\endgroup\$ Feb 18, 2016 at 17:09
  • 1
    \$\begingroup\$ @CaptainMan: More likely, they first pressed the {} button, and then pasted in their code. \$\endgroup\$ Feb 20, 2016 at 21:29
  • \$\begingroup\$ @CaptainMan.. you are so correct. I would take care from next time. \$\endgroup\$
    – Mosbius8
    Feb 21, 2016 at 12:46

3 Answers 3


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.

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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