Java Regex String Replace Whitespace replaceWhitespace(String searchTerm)

Here you can find the source of replaceWhitespace(String searchTerm)

Description

Replaces whitespace characters with underline.

License

Open Source License

Declaration

public static String replaceWhitespace(String searchTerm) 

Method Source Code

//package com.java2s;
/*//from  w  ww .  j a  v a2  s. c om
 * #%L
 * Cytoscape Search Impl (search-impl)
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2006 - 2013 The Cytoscape Consortium
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 2.1 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
 * #L%
 */

import java.util.regex.Pattern;

public class Main {
    public static final String SEARCH_STRING = "\\s";
    public static final String REPLACE_STRING = "_";

    /**
     * Replaces whitespace characters with underline. Method: Search for
     * SEARCH_STRING, replace with REPLACE_STRING.
     */
    public static String replaceWhitespace(String searchTerm) {
        String replaceTerm = "";

        if (searchTerm == null) {
            return replaceTerm;
        }

        Pattern searchPattern = Pattern.compile(SEARCH_STRING);

        String[] result = searchPattern.split(searchTerm);
        replaceTerm = result[0];
        for (int i = 1; i < result.length; i++) {
            replaceTerm = replaceTerm + REPLACE_STRING + result[i];
        }

        return replaceTerm;
    }
}

Related

  1. replaceBlank(String str)
  2. replaceBlank(String str)
  3. replaceBlank(String str)
  4. replaceWhitespace(final String value, final boolean stripExtras)
  5. replaceWhitespace(final String value, final boolean stripExtras)
  6. replaceWhiteSpaces(final String value, final String replaceBy)
  7. replaceWhitespaces(String inString, String with)
  8. replaceWhitespaces(String inString, String with)
  9. replaceWhiteSpaces(String str)