Java Utililty Methods String Sub String

List of utility methods to do String Sub String

Description

The list of methods to do String Sub String are organized into topic(s).

Method

int[]substract(int[] array1, int[] array2)
Returns the element-wise substraction of two arrays.
int[] out = new int[array1.length];
for (int i = 0; i < out.length; i++)
    out[i] = array1[i] - array2[i];
return out;
Numbersubstract(Number n1, Number n2)
Substract two numbers
Class<?> type = getComputationType(n1, n2);
Number val1 = convertTo(n1, type);
Number val2 = convertTo(n2, type);
if (type == Long.class)
    return Long.valueOf(val1.longValue() - val2.longValue());
return new Double(val1.doubleValue() - val2.doubleValue());
double[]substraction2(double[] a, double[] b)
get the difference of two 2-dimension vector
return new double[] { a[0] - b[0], a[1] - b[1] };
StringsubstractPrefixPostfix(Object obj, String prefix, String suffix)
This class reflectively looks for the given postfix and removes it from the classname of the given object.
if (obj == null) {
    return "";
String className = obj instanceof Class ? ((Class<?>) obj).getSimpleName() : obj.getClass().getSimpleName();
if (!className.startsWith(suffix) && !className.endsWith(suffix)) {
    return className;
String name = className.substring(prefix.length(), className.length() - suffix.length());
...
StringsubStrBeforeDotNotIncludeDot(String str)
sub Str Before Dot Not Include Dot
String result = str;
int iPos = result.indexOf(".");
if (iPos != -1) {
    result = result.substring(0, iPos);
return result;
StringsubStrByBytes(String str, int len, String tail)
sub Str By Bytes
if (str == null) {
    return "";
if (str.getBytes().length <= len) {
    return str.trim();
str = str.trim();
String s = "";
...
StringsubstrByLength(String str, int start, int size, String markCode)
substr By Length
String finalStr = "";
if (str == null) {
    for (int i = 0; i < size; i++) {
        finalStr = finalStr + " ";
        return finalStr;
int length = start + size - 1;
...
String[]subStrBytes2(String str, int byteLength)
sub Str Bytes
String[] result = new String[] { "", "" };
if (str != null && getStringByteLength(str) > byteLength) {
    String tmp = "";
    char c;
    int iCount = 0;
    int i = 0;
    for (i = 0; iCount < byteLength; i++) {
        c = str.charAt(i);
...
intsubstrCount(String haystack, String needle)
Returns the count of the substring
int lastIndex = 0;
int count = 0;
if (needle != null && haystack != null) {
    while (lastIndex != -1) {
        lastIndex = haystack.indexOf(needle, lastIndex);
        if (lastIndex != -1) {
            count++;
            lastIndex += needle.length();
...
StringsubStrIfNeed(final String str, int num)
sub Str If Need
if (strIsNullOrEmpty(str)) {
    return str;
int len = str.length();
if (len <= num) {
    return str;
return str.substring(0, num);
...