StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for StringUtil.java

Source

/*
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://wingsframework.org).
 *
 * wingS 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.
 *
 * Please see COPYING for the complete licence.
 */

import java.util.StringTokenizer;

/**
 * Some string manipulation utilities.
 *
 * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
 */
public class StringUtil {

    /**
     * replaces all newlines in the given String 's' with the replacement
     * string 'r'. Each line is trimmed from leading and trailing whitespaces,
     * then the new line-delimiter is added.
     *
     * @param s the source string.
     * @param r the new line delimiter
     * @return the resulting string.
     */
    public static final String replaceNewLines(String s, String r) {
        StringBuilder result = new StringBuilder();

        StringTokenizer t = new StringTokenizer(s, "\n");
        while (t.hasMoreTokens()) {
            result.append(t.nextToken().trim()).append(r);
        }
        return result.toString();
    }

}