Java String Underscore underscoreSpaces(String input)

Here you can find the source of underscoreSpaces(String input)

Description

underscore Spaces

License

Open Source License

Declaration

public static String underscoreSpaces(String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String underscoreSpaces(String input) {
        if (input == null)
            return null;
        String tmp = input.trim().replaceAll("\\s+", "_");
        tmp = tmp.replaceAll("_+", "_");
        return tmp;
    }//ww w .j av a2 s.  c  o m

    /**
     * will trim off the front and back of the string.
     * 
     * if trim = " " then it is the same as str.trim()
     * 
     * @param input
     * @param trim
     * @return
     */
    public static String trim(String input, String trim) {
        if (input == null || trim == null || trim.isEmpty())
            return input;

        String tmp = input;
        while (tmp.startsWith(trim)) {
            tmp = tmp.substring(trim.length());
        }
        while (tmp.endsWith(trim)) {
            tmp = tmp.substring(0, tmp.length() - trim.length());
        }
        return tmp;

    }
}

Related

  1. underScoreCase2CamelCase(String str)
  2. underscored(String string)
  3. underscoredToCamel(String string)
  4. underscoreName(String camelCaseName)
  5. underscoreName(String name)
  6. underscoreSpaces(String str)