⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 strings.java

📁 一个自然语言处理的Java开源工具包。LingPipe目前已有很丰富的功能
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 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 3.6 * @since   LingPipe1.0 * @see     java.lang.Character * @see     java.lang.String * @see     java.lang.StringBuffer */public class Strings {    /**     * Forbid instance construction.     */    private Strings() {        /* no instances */    }    /**     * String representing the charset consisting of UTF 8 encoded     * unicode characters.     */    public static String UTF8 = "UTF-8";    /**     * Return a copy of the specified string, trimming any underlying     * array characters to render the resulting string of minimal     * size..     *     * @param s String to copy.     * @return Copy of specified string.     */    public static String save(String s) {        return new String(s.toCharArray());    }    /**     * Return the string that is the reverse of the specified     * character sequence.     */    public static String reverse(CharSequence cs) {        StringBuffer sb = new StringBuffer(cs.length());        for (int i = cs.length(); --i >= 0; )            sb.append(cs.charAt(i));        return sb.toString();    }    /**     * Returns <code>true</code> if the specified string contains     * an instance of the specified character.     *     * @param s String to check for character.     * @param c Character.     * @return <code>true</code> if specified character occurs in     * specified string.     */    public static boolean containsChar(String s, char c) {        return s.indexOf(c) >= 0;    }    /**     * Returns <code>true</code> if the specified buffer contains     * only whitespace characters.     *     * @param sb String buffer to test for whitespace.     * @return <code>true</code> if the specified buffer contains only     * whitespace characters.     */    public static boolean allWhitespace(StringBuffer sb) {        return allWhitespace(sb.toString());    }    /**     * Returns <code>true</code> if the specified string contains     * only whitespace characters.     *     * @param s Stirng to test for whitespace.     * @return <code>true</code> if the specified string contains only     * whitespace characters.     */    public static boolean allWhitespace(String s) {        return allWhitespace(s.toCharArray(),0,s.length());    }    /**     * Returns <code>true</code> if the specified range of the     * specified character array only whitespace characters, as defined for     * characters by {@link #isWhitespace(char c)}.     *     * @param ch Character array to test for whitespace characters in range.     * @param start Beginning of range to test.     * @param length Number of characters to test.     * @return <code>true</code> if the specified string contains only     * whitespace characters.     */    public static boolean allWhitespace(char[] ch, int start, int length) {        for (int i = start; i < start+length; ++i)            if (!isWhitespace(ch[i])) return false;        return true;    }    /**     * Returns true if specified character is a whitespace character.     * The definition in {@link     * java.lang.Character#isWhitespace(char)} is extended to include     * the unicode non-breakable space character (unicode 160).     *     * @param c Character to test.     * @return <code>true</code> if specified character is a     * whitespace.     * @see java.lang.Character#isWhitespace(char)     */    public static boolean isWhitespace(char c) {        return Character.isWhitespace(c) || c == NBSP_CHAR;    }    /**     * Appends a whitespace-normalized form of the specified character     * sequence into the specified string buffer.  Initial and final     * whitespaces are not appended, and every other maximal sequence     * of contiguous whitespace is replaced with a single whitespace     * character.  For instance, <code>&quot; a\tb\n&quot;</code>     * would append the following characters to <code>&quot;a     * b&quot;</code>.     *     * <P>This command is useful for text inputs for web or GUI     * applications.     *     * @param cs Character sequence whose normalization is appended to     * the buffer.     * @param sb String buffer to which the normalized character     * sequence is appended.     */    public static void normalizeWhitespace(CharSequence cs, StringBuffer sb) {        int i = 0;        int length = cs.length();        while (length > 0 && isWhitespace(cs.charAt(length-1)))            --length;        while (i < length && isWhitespace(cs.charAt(i)))            ++i;        boolean inWhiteSpace = false;        for ( ; i < length; ++i) {            char nextChar = cs.charAt(i);            if (isWhitespace(nextChar)) {                if (!inWhiteSpace) {                    sb.append(' ');                    inWhiteSpace = true;                }            } else {                inWhiteSpace = false;                sb.append(nextChar);            }        }    }    /**     * Returns a whitespace-normalized version of the specified     * character sequence.  See {@link     * #normalizeWhitespace(CharSequence,StringBuffer)} for     * information on the normalization procedure.     *     * @param cs Character sequence to normalize.     * @return Normalized version of character sequence.     */    public static String normalizeWhitespace(CharSequence cs) {        StringBuffer sb = new StringBuffer();        normalizeWhitespace(cs,sb);        return sb.toString();    }    /**     * Returns <code>true</code> if all of the characters     * making up the specified string are digits.     *     * @param s String to test.     * @return <code>true</code> if all of the characters making up     * the specified string are digits.     */    public static boolean allDigits(String s) {        return allDigits(s.toCharArray(),0,s.length());    }    /**     * Returns <code>true</code> if all of the characters     * in the specified range are digits.     *     * @param cs Underlying characters to test.     * @param start Index of first character to test.     * @param length Number of characters to test.     * @return <code>true</code> if all of the characters making up     * the specified string are digits.     */    public static boolean allDigits(char[] cs, int start, int length) {        for (int i = 0; i < length; ++i)            if (!Character.isDigit(cs[i+start])) return false;        return true;    }    /**     * Returns true if specified character is a punctuation character.     * Punctuation includes comma, period, exclamation point, question     * mark, colon and semicolon.  Note that quotes and apostrophes     * are not considered punctuation by this method.     *     * @param c Character to test.     * @return <code>true</code> if specified character is a     * whitespace.     * @see java.lang.Character     */    public static boolean isPunctuation(char c) {        return c == ','            || c == '.'            || c == '!'            || c == '?'            || c == ':'            || c == ';'            ;    }    /**     * Returns the result of concatenating the specified number of     * copies of the specified string.  Note that there are no spaces     * inserted between the specified strings in the output.     *     * @param s String to concatenate.     * @param count Number of copies of string to concatenate.     * @return Specified string concatenated with itself the specified     * number of times.     */    public static String power(String s, int count) {        StringBuffer sb = new StringBuffer();        for (int i = 0; i < count; ++i)            sb.append(s);        return sb.toString();    }    /**     * Concatenate the elements of the specified array as strings,     * separating with the default separator {@link     * #DEFAULT_SEPARATOR_STRING}.     *     * @param xs Array of objects whose string representations are     * concatenated.     * @return Concatenation of string representations of specified     * objects separated by the default separator.     */    public static String concatenate(Object[] xs) {        return concatenate(xs,DEFAULT_SEPARATOR_STRING);    }    /**     * Concatenate the elements of the specified array as strings,     * separating with the specified string spacer.     *     * @param xs Array of objects whose string representations are     * concatenated.     * @param spacer String to insert between the string     * representations.     * @return Concatenation of string representations of specified     * objects separated by the specified spacer.     */    public static String concatenate(Object[] xs, String spacer) {        return concatenate(xs,0,spacer);    }    /**     * Concatenate the elements of the specified array as strings,     * starting at the object at the specified index and continuing     * through the rest of the string, separating with the specified     * string spacer.     *     * @param xs Array of objects whose string representations are     * concatenated.     * @param start Index of first object to include.     * @param spacer String to insert between the string     v     * representations.     * @return Concatenation of string representations of specified     * objects separated by the specified spacer.     */    public static String concatenate(Object[] xs, int start,                                     String spacer) {        return concatenate(xs,start,xs.length,spacer);    }    /**     * Concatenate the elements of the specified array as strings,     * starting at the object at the specified index and continuing     * through one element before the specified end index, separating     * with the default spacer {@link #DEFAULT_SEPARATOR_STRING}.     *     * @param xs Array of objects whose string representations are     * concatenated.     * @param start Index of first object to include.     * @param end The index of the last element to include plus     * <code>1</code>.     * @return Concatenation of string representations of specified     * objects separated by the specified spacer.     */    public static String concatenate(Object[] xs, int start, int end) {        return concatenate(xs,start,end,DEFAULT_SEPARATOR_STRING);    }    /**     * Concatenate the elements of the specified array as strings,     * starting at the object at the specified index and continuing     * through one element before the specified end index, separating     * with the specified spacer.     *     * @param xs Array of objects whose string representations are     * concatenated.     * @param start Index of first object to include.     * @param end The index of the last element to include plus     * <code>1</code>.     * @param spacer String to insert between the string     * representations.     * @return Concatenation of string representations of specified     * objects separated by the specified spacer.     */    public static String concatenate(Object[] xs, int start, int end,                                     String spacer) {        StringBuffer sb = new StringBuffer();        for (int i = start; i < end; ++i) {            if (i > start) sb.append(spacer);            sb.append(xs[i]);        }        sb.setLength(sb.length());        return sb.toString();    }    /**     * Appends an ``indentation'' to the specified string buffer,     * consisting of a newline character and the specified number of     * space characters to the specified string buffer.     *     * @param sb String buffer to indent.     * @param length Number of spaces to append after a newline to the     * specified string buffer.     */    public static void indent(StringBuffer sb, int length) {        sb.append(NEWLINE_CHAR);        padding(sb,length);    }    /**     * Return a string consisting of the initial segment of the     * specified string trimmed or padded with spaces to fit the     * specified length.     *     * @param in String to fit to specified length.     * @param length Length to fit.     * @return String fitted to specified length.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -