Java String Sub String subStrIfNeed(final String str, int num)

Here you can find the source of subStrIfNeed(final String str, int num)

Description

sub Str If Need

License

Apache License

Declaration

public static String subStrIfNeed(final String str, int num) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String subStrIfNeed(final String str, int num) {
        if (strIsNullOrEmpty(str)) {
            return str;
        }//from   w  w w.j  av  a2  s .c  o  m
        int len = str.length();
        if (len <= num) {
            return str;
        }
        return str.substring(0, num);
    }

    /**
     * Judge string is null or empty.
     *
     * @param str str
     * @return boolean
     */
    public static boolean strIsNullOrEmpty(final String str) {
        if (str == null || "".equals(str)) {
            return true;
        }

        return false;
    }
}

Related

  1. subStrBeforeDotNotIncludeDot(String str)
  2. subStrByBytes(String str, int len, String tail)
  3. substrByLength(String str, int start, int size, String markCode)
  4. subStrBytes2(String str, int byteLength)
  5. substrCount(String haystack, String needle)
  6. substring(byte[] array, int start)
  7. substring(byte[] src, int start, int len)
  8. substring(char[] s, int start, int end)
  9. substring(final String pSource, final String pBeginBoundaryString, final String pEndBoundaryString, final int pOffset)