stringutils.java

来自「Mandarax是一个规则引擎的纯Java实现。它支持多类型的事实和基于反映的规」· Java 代码 · 共 115 行

JAVA
115
字号
/*
 * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.util;


import java.util.Collection;
import java.util.Iterator;

/**
 * Contains useful string utilities.
 * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
 * @version 3.4 <7 March 05>
 * @since 1.1
 */
public final class StringUtils {

    // this constant defines the separator used for printing lists
    public static String SEPARATOR = ",";

    /**
     * Get a print string for an array based on the
     * print strings of the objects.
     * @return the produced string
     * @param objs the objects
     */
    public static String toString(Object[] objs) {
        if(objs == null) {
            return "?";
        }

        boolean      first = true;
        StringBuffer buf   = new StringBuffer ();

        for(int i = 0; i < objs.length; i++) {
            if(first) {
                first = false;
            } else {
                buf.append (SEPARATOR);
            }

            buf.append ((objs[i] == null)
                        ? "?"
                        : objs[i].toString ());
        }

        return new String (buf);
    }

    /**
     * Get a print string for a collection based on the
     * print strings of the objects it contains.
     * @return the produced string
     * @param coll a collection
     */
    public static String toString(Collection coll) {
        return toString (coll.iterator ());
    }

    /**
     * Get a print string for an iterator based on the
     * print strings of the objects.
     * @return the produced string
     * @param it an iterator (should be new initialized in order to make sure that we include all objects)
     */
    public static String toString(Iterator it) {
        if(it == null) {
            return "?";
        }

        boolean      first = true;
        StringBuffer buf   = new StringBuffer ();

        for(Iterator ii = it; ii.hasNext (); ) {
            if(first) {
                first = false;
            } else {
                buf.append (SEPARATOR);
            }

            buf.append (ii.next ().toString ());
        }

        return new String (buf);
    }
    /**
     * Split a string. E.g., calling this method with "23"+"54","+" yields {"23","54"}
     * @param txt a string
     * @param sep a separator
     * @return an array of two strings or null if the separator has not be found 
     */
    public static String[] split(String txt,String sep) {
    	int position = txt.indexOf(sep);
    	if (position==-1) return null;
    	String[] parts = new String[2];
    	parts[0] = txt.substring(0,position);
    	parts[1] = txt.substring(position+sep.length());
    	return parts;
    }
}

⌨️ 快捷键说明

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