Java String Sub String subString(String s, int startIndex, int count)

Here you can find the source of subString(String s, int startIndex, int count)

Description

slice up the given string

License

Open Source License

Parameter

Parameter Description
s , giver string
startIndex a parameter
count , max size of substring

Return

substring

Declaration

public static String subString(String s, int startIndex, int count) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w w  .  jav a 2  s. c o m
     * slice up the given string
     * @param s , giver string
     * @param startIndex
     * @param count , max size of substring
     * @return substring
     */
    public static String subString(String s, int startIndex, int count) {
        if (isEmpty(s)) {
            return "";
        }
        byte[] stringBytes = s.getBytes();
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (count > stringBytes.length - startIndex) {
            count = stringBytes.length - startIndex;
        }
        return new String(stringBytes, startIndex, count);
    }

    /**
     * <p>
     * Checks if a CharSequence is empty ("") or null.
     * </p>
     *
     * <pre>
     * StringUtil.isEmpty(null)      = true
     * StringUtil.isEmpty("")        = true
     * StringUtil.isEmpty(" ")       = false
     * StringUtil.isEmpty("bob")     = false
     * StringUtil.isEmpty("  bob  ") = false
     * </pre>
     *
     * @param cs
     * @return
     */
    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

Related

  1. substring(String s, int i, String substring, int direction)
  2. subString(String s, int length)
  3. substring(String s, int start)
  4. substring(String s, int start, int length)
  5. substring(String s, int start, int stop)
  6. substring(String self, Integer startIndex, Integer endIndex)
  7. substring(String source, int beginIndex, int endIndex)
  8. substring(String source, int length, String padding)
  9. substring(String source, int offset, int dataLength)