Java String Sub String substrings(String[] arr, int start, int end)

Here you can find the source of substrings(String[] arr, int start, int end)

Description

Return specified substrings for all the members of the given array.

License

Open Source License

Parameter

Parameter Description
arr the array of strings to make the substrings of
start the starting index of the substring (incl.)
end the ending index of the substring (excl.)

Return

substrings of the specified range for all the strings in the given array

Declaration

public static String[] substrings(String[] arr, int start, int end) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from ww w .  j a va2s . c  om
     * Return specified substrings for all the members of the given array. If the strings are not long enough,
     * returns the longest possible part.
     *
     * @param arr the array of strings to make the substrings of
     * @param start the starting index of the substring (incl.)
     * @param end the ending index of the substring (excl.)
     * @return substrings of the specified range for all the strings in the given array
     */
    public static String[] substrings(String[] arr, int start, int end) {

        String[] subs = new String[arr.length];

        for (int i = 0; i < arr.length; ++i) {
            subs[i] = safeSubstr(arr[i], start, end);
        }
        return subs;
    }

    /**
     * This returns a substring of the given string even if the string is not long enough (the substring will be truncated then).
     * @param str the string to take the substring of
     * @param start the starting index
     * @param end the ending index
     */
    public static String safeSubstr(String str, int start, int end) {
        if (str.length() >= end) {
            return str.substring(start, end);
        } else if (str.length() >= start) {
            return str.substring(start);
        } else {
            return "";
        }
    }
}

Related

  1. subStringNobit(String str, int toCount, String more)
  2. subStringNotEncode(String subject, int size)
  3. subStringRight(String str, int length)
  4. substrings(String str, int start, int end)
  5. subStrings(String str1, String str2)
  6. subStringToInteger(String src, String start, String to)
  7. substringToLast(final String str, final String separator)
  8. substringUntil(String org, int begin, char term)
  9. substringUntilMatch(String string, String match)