6
\$\begingroup\$

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();
    }
}
\$\endgroup\$
0

3 Answers 3

55
\$\begingroup\$

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.

\$\endgroup\$
4
  • 8
    \$\begingroup\$ URLEncoder in particular, encodes space as + which may be undesireable in some cases. \$\endgroup\$
    – nitro2k01
    Commented Feb 18, 2016 at 11:56
  • 15
    \$\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
    Commented Feb 18, 2016 at 12:56
  • 6
    \$\begingroup\$ URLEncoder is for form encoding. URL escaping is done with java.net.URI. \$\endgroup\$
    – VGR
    Commented Feb 18, 2016 at 15:11
  • 2
    \$\begingroup\$ @VGR Amazing that this got so many upvotes being wrong... \$\endgroup\$
    – jpmc26
    Commented Feb 19, 2016 at 20:15
28
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

}
\$\endgroup\$
2
  • 6
    \$\begingroup\$ While good advice in general, it has nothing to do with the question as posted. \$\endgroup\$
    – fluffy
    Commented 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\$ Commented Feb 18, 2016 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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