For some tasks, you're presented an opportunity to do things the easy way or the hard way. When your friend is proudly showing off his work at replacing a light fixture, the switch, and all the wiring, you might wonder why not just replace the dead bulb?

"I was just fixing a bug in our software, and I came across this function," Johnny A. writes. "It works perfectly — does exactly what it says on the tin. I can't help thinking it could have been done with a few less lines of code, however. One, for example."

public static String replaceSpaceWithUnderscore(String str)
{
    if (str == null || str.length() == 0)
    {
        return str;
    }

    StringTokenizer tokenizer = new StringTokenizer(str, " ");
    String token = "";
    String newStr = "";
    int count = 1;
    while (tokenizer.hasMoreTokens())
    {
        token = tokenizer.nextToken();
        if (count == 1)
        {
            newStr = token;
        } else {
            newStr = newStr + "_" + token;
        }
        count++;
    }
    return newStr;
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!