Java - Write code to Convert space characters by underscore characters.

Requirements

Write code to Convert space characters by underscore characters.

test test becomes test_test

Hint

use String.replace() method

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String content = "book2s.com";
        System.out.println(replaceSpaceUnderscoreChar(content));
    }//from  w  w  w .jav  a2  s  .  co m

    protected final static char UNDERSCORE_CHAR = '_';

    /**
     * <p>Convert space characters by underscore characters. This method is used in 
     * creating id or handle name from common expression.</p>
     * @param content original content with space characters
     * @return string or identifiers with underscore characters
     */
    public static String replaceSpaceUnderscoreChar(String content) {
        return content.replace(' ', UNDERSCORE_CHAR);
    }
}

Related Exercise