Here you can find the source of difference(String str1, String str2)
Compares two Strings, and returns the portion where they differ.
Parameter | Description |
---|---|
str1 | the first String, may be null |
str2 | the second String, may be null |
public static String difference(String str1, String str2)
//package com.java2s; /*//from w w w.ja va 2 s. c o m * Copyright (c) 2007-2016 AREasy Runtime * * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software"); * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either version 2.1 of the License, * or (at your option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT, * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ public class Main { /** * The empty String <code>""</code>. */ public static final String EMPTY = ""; /** * <p>Compares two Strings, and returns the portion where they differ. * (More precisely, return the remainder of the second String, * starting from where it's different from the first.)</p> * <p/> * <p>For example, * <code>difference("i am a machine", "i am a robot") -> "robot"</code>.</p> * <p/> * <pre> * StringUtility.difference(null, null) = null * StringUtility.difference("", "") = "" * StringUtility.difference("", "abc") = "abc" * StringUtility.difference("abc", "") = "" * StringUtility.difference("abc", "abc") = "" * StringUtility.difference("ab", "abxyz") = "xyz" * StringUtility.difference("abcde", "abxyz") = "xyz" * StringUtility.difference("abcde", "xyz") = "xyz" * </pre> * * @param str1 the first String, may be null * @param str2 the second String, may be null * @return the portion of str2 where it differs from str1; returns the * empty String if they are equal */ public static String difference(String str1, String str2) { if (str1 == null) return str2; if (str2 == null) return str1; int at = indexOfDifference(str1, str2); if (at == -1) return EMPTY; return str2.substring(at); } /** * <p>Compares two Strings, and returns the index at which the * Strings begin to differ.</p> * <p/> * <p>For example, * <code>indexOfDifference("i am a machine", "i am a robot") -> 7</code></p> * <p/> * <pre> * StringUtility.indexOfDifference(null, null) = -1 * StringUtility.indexOfDifference("", "") = -1 * StringUtility.indexOfDifference("", "abc") = 0 * StringUtility.indexOfDifference("abc", "") = 0 * StringUtility.indexOfDifference("abc", "abc") = -1 * StringUtility.indexOfDifference("ab", "abxyz") = 2 * StringUtility.indexOfDifference("abcde", "abxyz") = 2 * StringUtility.indexOfDifference("abcde", "xyz") = 0 * </pre> * * @param str1 the first String, may be null * @param str2 the second String, may be null * @return the index where str2 and str1 begin to differ; -1 if they are equal */ public static int indexOfDifference(String str1, String str2) { if (str1 == str2) return -1; if (str1 == null || str2 == null) return 0; int i; for (i = 0; i < str1.length() && i < str2.length(); ++i) { if (str1.charAt(i) != str2.charAt(i)) break; } if (i < str2.length() || i < str1.length()) { return i; } return -1; } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * <p/> * <p>A negative start position can be used to start <code>n</code> * characters from the end of the String.</p> * <p/> * <p>A <code>null</code> String will return <code>null</code>. * An empty ("") String will return "".</p> * <p/> * <pre> * StringUtility.substring(null, *) = null * StringUtility.substring("", *) = "" * StringUtility.substring("abc", 0) = "abc" * StringUtility.substring("abc", 2) = "c" * StringUtility.substring("abc", 4) = "" * StringUtility.substring("abc", -2) = "bc" * StringUtility.substring("abc", -4) = "abc" * </pre> * * @param str the String to get the substring from, may be null * @param start the position to start from, negative means * count back from the end of the String by this many characters * @return substring from start position, <code>null</code> if null String input */ public static String substring(String str, int start) { if (str == null) return null; // handle negatives, which means last n characters if (start < 0) start = str.length() + start; // remember start is negative if (start < 0) start = 0; if (start > str.length()) return EMPTY; return str.substring(start); } /** * <p>Gets a substring from the specified String avoiding exceptions.</p> * <p/> * <p>A negative start position can be used to start/end <code>n</code> * characters from the end of the String.</p> * <p/> * <p>The returned substring starts with the character in the <code>start</code> * position and ends before the <code>end</code> position. All position counting is * zero-based -- i.e., to start at the beginning of the string use * <code>start = 0</code>. Negative start and end positions can be used to * specify offsets relative to the end of the String.</p> * <p/> * <p>If <code>start</code> is not strictly to the left of <code>end</code>, "" * is returned.</p> * <p/> * <pre> * StringUtility.substring(null, *, *) = null * StringUtility.substring("", * , *) = ""; * StringUtility.substring("abc", 0, 2) = "ab" * StringUtility.substring("abc", 2, 0) = "" * StringUtility.substring("abc", 2, 4) = "c" * StringUtility.substring("abc", 4, 6) = "" * StringUtility.substring("abc", 2, 2) = "" * StringUtility.substring("abc", -2, -1) = "b" * StringUtility.substring("abc", -4, 2) = "ab" * </pre> * * @param str the String to get the substring from, may be null * @param start the position to start from, negative means * count back from the end of the String by this many characters * @param end the position to end at (exclusive), negative means * count back from the end of the String by this many characters * @return substring from start position to end positon, * <code>null</code> if null String input */ public static String substring(String str, int start, int end) { if (str == null) return null; // handle negatives if (end < 0) end = str.length() + end; // remember end is negative if (start < 0) start = str.length() + start; // remember start is negative // check length next if (end > str.length()) end = str.length(); // if start is greater than end, return "" if (start > end) return EMPTY; if (start < 0) start = 0; if (end < 0) end = 0; return str.substring(start, end); } }