Strings.java Source code

Java tutorial

Introduction

Here is the source code for Strings.java

Source

/*
 * LingPipe v. 3.9
 * Copyright (C) 2003-2010 Alias-i
 *
 * This program is licensed under the Alias-i Royalty Free License
 * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i
 * Royalty Free License Version 1 for more details.
 *
 * You should have received a copy of the Alias-i Royalty Free License
 * Version 1 along with this program; if not, visit
 * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact
 * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211,
 * +1 (718) 290-9170.
 */

//package com.aliasi.util;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import java.text.DecimalFormat;

/**
 * Static utility methods for processing strings, characters and
 * string buffers.
 *
 * @author  Bob Carpenter
 * @version 4.0.1
 * @since   LingPipe1.0
 * @see     java.lang.Character
 * @see     java.lang.String
 * @see     java.lang.StringBuilder
 */
public class Strings {

    /**
     * Returns an array of substrings of the specified string,
     * in order, with divisions before and after any instance
     * of the specified character.  The returned array will always
     * have at least one element.  Elements in the returned array
     * may be empty.  The following examples illustrate this behavior:
     *
     * <br/><br/>
     * <table border="1" cellpadding="5">
     * <tr><td><b>Call</b></td><td><b>Result</b></td></tr>
     * <tr>
     *   <td><code>split("",' ')</code></td>
     *   <td><code>{ "" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split("a",' ')</code></td>
     *   <td><code>{ "a" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split("a b",' ')</code></td>
     *   <td><code>{ "a", "b" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split("aaa bb cccc",' ')</code></td>
     *   <td><code>{ "aaa", "bb", "cccc" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split(" a",' ')</code></td>
     *   <td><code>{ "", "a" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split("a ",' ')</code></td>
     *   <td><code>{ "a", "" }</code></td>
     * </tr>
     * <tr>
     *   <td><code>split(" a ",' ')</code></td>
     *   <td><code>{ "", "a", "" }</code></td>
     * </tr>
     * </table>
     *
     * @param s String to split.
     * @param c Character on which to split the string.
     * @return The array of substrings resulting from splitting the
     * specified string on the specified character.
     */
    public static String[] split(String s, char c) {
        char[] cs = s.toCharArray();
        int tokCount = 1;
        for (int i = 0; i < cs.length; ++i)
            if (cs[i] == c)
                ++tokCount;
        String[] result = new String[tokCount];
        int tokIndex = 0;
        int start = 0;
        for (int end = 0; end < cs.length; ++end) {
            if (cs[end] == c) {
                result[tokIndex] = new String(cs, start, end - start);
                ++tokIndex;
                start = end + 1;
            }
        }
        result[tokIndex] = new String(cs, start, cs.length - start);
        return result;
    }

}