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)
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 |
public static boolean substringEquals(final String s1, final int fromIndex1, final int toIndex1, final String s2, final int fromIndex2, final int toIndex2)
//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; } }