阿里巴巴Java机试题

阿里巴巴机试题,根据注释里的方法调用的结果,写全方法的实现代码,一共四个方法

public class StringUtils {
    /**
     * StringUtils.join(null, *) = null 
     * StringUtils.join([], *) = ""
     * StringUtils.join([null], *) = "" 
     * StringUtils.join(["a", "b", "c"], ';') = "a;b;c" 
     * StringUtils.join(["a", "b", "c"], null) = "abc"
     * StringUtils.join([null, "", "a"], ';') = ";;a"
     */
     public static String join(Object[] array, char separator) {

         return null;
     }
     /**
     * <p>Left pad a String with a specified character.</p>
     *
     * <p>Pad to a size of {@code size}.</p>
     *
     * <pre>
     * StringUtils.leftPad(null, *, *)     = null
     * StringUtils.leftPad("", 3, 'z')     = "zzz"
     * StringUtils.leftPad("bat", 3, 'z')  = "bat"
     * StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
     * StringUtils.leftPad("bat", 1, 'z')  = "bat"
     * StringUtils.leftPad("bat", -1, 'z') = "bat"
     * </pre>
     *
     * @param str  the String to pad out, may be null
     * @param size  the size to pad to
     * @param padChar  the character to pad with
     * @return left padded String or original String if no padding is necessary,
     *  {@code null} if null String input
     * @since 2.0
     */
    public static String leftPad(String str, int size, String padChar) {
    
       return null;
    }
    
    /**
     * <p>Right pad a String with a specified character.</p>
     *
     * <p>The String is padded to the size of {@code size}.</p>
     *
     * <pre>
     * StringUtils.rightPad(null, *, *)     = null
     * StringUtils.rightPad("", 3, 'z')     = "zzz"
     * StringUtils.rightPad("bat", 3, 'z')  = "bat"
     * StringUtils.rightPad("bat", 5, 'z')  = "batzz"
     * StringUtils.rightPad("bat", 1, 'z')  = "bat"
     * StringUtils.rightPad("bat", -1, 'z') = "bat"
     * </pre>
     *
     * @param str  the String to pad out, may be null
     * @param size  the size to pad to
     * @param padChar  the character to pad with
     * @return right padded String or original String if no padding is necessary,
     *  {@code null} if null String input
     * @since 2.0
     */
    public static String rightPad(String str, int size, String padChar) {
  
       return null;
    }
    /**
     * <p>Capitalizes a String changing the first letter to title case as
     * per {@link Character#toTitleCase(char)}. No other letters are changed.</p>
     *
    
     * <pre>
     * StringUtils.capitalize(null)  = null
     * StringUtils.capitalize("")    = ""
     * StringUtils.capitalize("cat") = "Cat"
  * StringUtils.capitalize("cAt") = "CAt"
     * </pre>
     *
     * @param str the String to capitalize, may be null
     * @return the capitalized String, {@code null} if null String input
     * @since 2.0
     */
    public static String capitalize(String str) {
 
      return null;
    }
    public static void main(String[] args) {
        
      }
}


public class StringUtils {
    /**

     * StringUtils.join(null, *) = null

     * StringUtils.join([], *) = ""

     * StringUtils.join([null], *) = ""

     * StringUtils.join(["a", "b", "c"], ';') = "a;b;c"

     * StringUtils.join(["a", "b", "c"], null) = "abc"

     * StringUtils.join([null, "", "a"], ';') = ";;a"

     */
    // FIXME 这里面第二个参数如果是char,根本传不进去null,所以改成Character
    public static String join(Object[] array, Character separator) {
        if(null != array) {
            if(array.length==0) return "";
            StringBuilder sb = new StringBuilder();
            boolean hasSep = false;
            for (Object a : array) {
                if (null != a) {
                    sb.append(a);
                }
                if (null != separator) {
                    hasSep = true;
                    sb.append(separator);
                }
            }
            if(hasSep) {
                return sb.substring(0, sb.length() - 1);
            } else{
                return sb.toString();
            }
        }
        return null;
    }

    /**

     * <p>Left pad a String with a specified character.</p>

     *

     * <p>Pad to a size of {@code size}.</p>

     *

     * <pre>

     * StringUtils.leftPad(null, *, *)     = null

     * StringUtils.leftPad("", 3, 'z')     = "zzz"

     * StringUtils.leftPad("bat", 3, 'z')  = "bat"

     * StringUtils.leftPad("bat", 5, 'z')  = "zzbat"

     * StringUtils.leftPad("bat", 1, 'z')  = "bat"

     * StringUtils.leftPad("bat", -1, 'z') = "bat"

     * </pre>

     *

     * @param str  the String to pad out, may be null

     * @param size  the size to pad to

     * @param padChar  the character to pad with

     * @return left padded String or original String if no padding is necessary,

     *  {@code null} if null String input

     * @since 2.0

     */

    public static String leftPad(String str, int size, String padChar) {
        if(null == str) return null;

        if(str.length()>=size) return str;
        else{
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<size-str.length();i++){
                sb.append(padChar);
            }
            sb.append(str);
            return sb.toString();
        }
    }



    /**

     * <p>Right pad a String with a specified character.</p>

     *

     * <p>The String is padded to the size of {@code size}.</p>

     *

     * <pre>

     * StringUtils.rightPad(null, *, *)     = null

     * StringUtils.rightPad("", 3, 'z')     = "zzz"

     * StringUtils.rightPad("bat", 3, 'z')  = "bat"

     * StringUtils.rightPad("bat", 5, 'z')  = "batzz"

     * StringUtils.rightPad("bat", 1, 'z')  = "bat"

     * StringUtils.rightPad("bat", -1, 'z') = "bat"

     * </pre>

     *

     * @param str  the String to pad out, may be null

     * @param size  the size to pad to

     * @param padChar  the character to pad with

     * @return right padded String or original String if no padding is necessary,

     *  {@code null} if null String input

     * @since 2.0

     */

    public static String rightPad(String str, int size, String padChar) {

        if(null == str) return null;

        if(str.length()>=size) return str;
        else{
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            for(int i=0;i<size-str.length();i++){
                sb.append(padChar);
            }
            return sb.toString();
        }
    }

    /**

     * <p>Capitalizes a String changing the first letter to title case as

     * per {@link Character#toTitleCase(char)}. No other letters are changed.</p>

     *



     * <pre>

     * StringUtils.capitalize(null)  = null

     * StringUtils.capitalize("")    = ""

     * StringUtils.capitalize("cat") = "Cat"

     * StringUtils.capitalize("cAt") = "CAt"

     * </pre>

     *

     * @param str the String to capitalize, may be null

     * @return the capitalized String, {@code null} if null String input

     * @since 2.0

     */

    public static String capitalize(String str) {
        if(null==str) return null;
        if(str.length()==0) return "";
        char[] cs=str.toCharArray();
        if(cs[0]>96&&cs[0]<123)
            cs[0]-=32;
        return String.valueOf(cs);
    }

    public static void main(String[] args) {
        System.out.println(StringUtils.join(null,'*'));

        System.out.println(StringUtils.join(new String[]{}, '*'));

        System.out.println(StringUtils.join(new String[]{null}, '*'));

        System.out.println(StringUtils.join(new String[]{"a", "b", "c"}, ';'));

        System.out.println(StringUtils.join(new String[]{"a", "b", "c"}, null));

        System.out.println(StringUtils.join(new String[]{null, "", "a"}, ';'));

        System.out.println(StringUtils.leftPad(null, 100, "*"));

        System.out.println(StringUtils.leftPad("", 3, "z"));

        System.out.println(StringUtils.leftPad("bat", 3, "z"));

        System.out.println(StringUtils.leftPad("bat", 5, "z"));

        System.out.println(StringUtils.leftPad("bat", 1, "z"));

        System.out.println(StringUtils.leftPad("bat", -1, "z"));

        System.out.println(StringUtils.rightPad(null, 100, "*"));

        System.out.println(StringUtils.rightPad("", 3, "z"));

        System.out.println(StringUtils.rightPad("bat", 3, "z"));

        System.out.println(StringUtils.rightPad("bat", 5, "z"));

        System.out.println(StringUtils.rightPad("bat", 1, "z"));

        System.out.println(StringUtils.rightPad("bat", -1, "z"));

        System.out.println(StringUtils.capitalize(null));

        System.out.println(StringUtils.capitalize(""));

        System.out.println(StringUtils.capitalize("cat"));

        System.out.println(StringUtils.capitalize("cAt"));

    }
}

哇,楼上辛苦了
先看一下spring的stringutils怎么实现的啊