Java String Sub String substringEquals(final String s1, final int fromIndex1, final int toIndex1, final String s2, final int fromIndex2, final int toIndex2)

Here you can find the source of substringEquals(final String s1, final int fromIndex1, final int toIndex1, final String s2, final int fromIndex2, final int toIndex2)

Description

Check equality of two substrings.

License

Apache License

Parameter

Parameter Description
s1 First string
fromIndex1 Starting index within s1
toIndex1 Ending index within s1
s2 Second string
fromIndex2 Starting index within s2
toIndex2 Ending index within s2

Return

boolean

Declaration

public static boolean substringEquals(final String s1, final int fromIndex1, final int toIndex1,
        final String s2, final int fromIndex2, final int toIndex2) 

Method Source Code

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

public class Main {
    /**/*ww w . j av a 2s  . c  o m*/
     * Check equality of two substrings. This method does not create
     * intermediate {@link String} objects and is roughly equivalent to:
     * <p/>
     * <pre>
     * <code>
     * String sub1 = s1.substring(fromIndex1, toIndex1);
     * String sub2 = s2.substring(fromIndex2, toIndex2);
     * sub1.equals(sub2);
     * </code>
     * </pre>
     *
     * @param s1 First string
     * @param fromIndex1 Starting index within {@code s1}
     * @param toIndex1 Ending index within {@code s1}
     * @param s2 Second string
     * @param fromIndex2 Starting index within {@code s2}
     * @param toIndex2 Ending index within {@code s2}
     * @return {@code boolean}
     */
    public static boolean substringEquals(final String s1, final int fromIndex1, final int toIndex1,
            final String s2, final int fromIndex2, final int toIndex2) {

        if (toIndex1 < fromIndex1) {
            throw new IndexOutOfBoundsException();
        }
        if (toIndex2 < fromIndex2) {
            throw new IndexOutOfBoundsException();
        }

        final int len1 = toIndex1 - fromIndex1;
        final int len2 = toIndex2 - fromIndex2;
        if (len1 != len2) {
            return false;
        }

        for (int i = 0; i < len1; ++i) {
            if (s1.charAt(fromIndex1 + i) != s2.charAt(fromIndex2 + i)) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. substringBytes(String value, int byte_len)
  2. substringCompare(String string, int index, char... characters)
  3. substringDelimited(String string, String start, String end, int startingPosition)
  4. substringEL(String str, int index, String defaultValue)
  5. substringEnd(String string, int start, int length)
  6. subStringExe(String s, String jmp, String sb, String se)
  7. substringFromMatch(String match, String string)
  8. substringFromMultiValuedFields(int start, int end, String[] fieldValues, int offsetGap, String interFieldJoiner)
  9. substringGuarded(String s, int position, int count)