/*
 * The baseCode project
 * 
 * Copyright (c) 2006 University of British Columbia
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package ubic.basecode.util;

import java.io.IOException;
import java.io.StringReader;

import org.apache.commons.lang.StringUtils;
import au.com.bytecode.opencsv.CSVReader;

/**
 * @author pavlidis
 * @version $Id$
 */
public class StringUtil {

    /**
     * @param numFields
     * @param line
     * @return
     */
    public static String[] csvSplit( String line ) {

        CSVReader reader = new CSVReader( new StringReader( line ) );

        try {
            return reader.readNext();
        } catch ( IOException e ) {
            throw new RuntimeException( e );
        }
    }

    /**
     * Method to wrap lines. It attempts to wrap on whitespace - it will not break within a word.
     * 
     * @param string
     * @param charPerLine
     * @param delim The string to put at the end of each new line. For example, "\n" or "&lt;br /&gt;";
     * @return
     */
    public static String wrap( String string, int charPerLine, String delim ) {
        if ( string == null || string.length() <= charPerLine ) return string;
        StringBuffer buf = new StringBuffer();
        char[] chars = string.toCharArray();
        int lastLineCount = 0;
        boolean needDelim = false;
        for ( int i = 0; i < chars.length; i++ ) {
            char c = chars[i];
            buf.append( c );
            lastLineCount++;

            if ( needDelim ) {
                if ( ( c == ' ' || c == '\t' || c == '\n' ) ) {
                    buf.append( delim );
                    lastLineCount = 0;
                    needDelim = false;
                    continue;
                }
            }

            if ( i > 0 && lastLineCount >= charPerLine ) {
                if ( ( c == ' ' || c == '\t' || c == '\n' ) ) {
                    buf.append( delim );
                    lastLineCount = 0;
                    needDelim = false;
                } else {
                    needDelim = true;
                }
            }
        }
        return buf.toString();
    }

    /**
     * Convert "Property" to "property"
     * 
     * @param input
     * @return the input with the first character in lowercase.
     */
    public static String lowerCaseFirstLetter( String input ) {
        return ( new String( new char[] { input.charAt( 0 ) } ) ).toLowerCase() + input.substring( 1 );
    }

    /**
     * Convert "property" to "Property"
     * 
     * @param input
     * @return the input with the first character in upper case..
     */
    public static String upperCaseFirstLetter( String input ) {
        return ( new String( new char[] { input.charAt( 0 ) } ) ).toUpperCase() + input.substring( 1 );
    }

    /**
     * @param stringi
     * @param stringj
     * @return
     */
    public static Object twoStringHashKey( String stringi, String stringj ) {
        // use arbitrary but consistent method for ordering.
        if ( stringi.hashCode() < stringj.hashCode() ) {
            return new Long( stringi.hashCode() | ( long ) stringj.hashCode() << 32 );
        }
        return new Long( stringj.hashCode() | ( long ) stringi.hashCode() << 32 );

    }

    /**
     * @param str the String to parse, may be <code>null</code>
     * @param separatorChar the separate character
     * @return an array of parsed Strings, <code>null</code> if null String input
     */
    public static String[] splitPreserveAllTokens( String str, char separatorChar ) {

        return StringUtils.splitPreserveAllTokens( str, separatorChar );
    }
}

@@ -1232,32 +1232,600 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * @param appendee The string to be added to%0A     * @param appendant The string to add to the end of the appendee%0A     * @param separator The string to put between the joined strings, if necessary.%0A     * @return appendee + separator + separator unless appendee is empty, in which case the appendant is returned.%0A     */%0A    public static String append( String appendee, String appendant, String separator ) %7B%0A        if ( StringUtils.isBlank( appendee ) ) %7B%0A            return appendant;%0A        %7D%0A        return appendee + separator + appendant;%0A%0A    %7D%0A%0A
     /**%0A     * M
@@ -4311,25 +4311,24 @@
 () %3C%3C 32 );%0A
-%0A
     %7D%0A%0A    /
@@ -4523,24 +4523,132 @@
 tring input%0A
+     * @deprecated Directly use %7B@link org.apache.commons.lang.StringUtils.splitPreserveAllTokens%7D instead.%0A
      */%0A    
@@ -4725,25 +4725,24 @@
 torChar ) %7B%0A
-%0A
         retu

add 'append' method