Noman Malik Noman Malik - 4 months ago 10
Android Question

match two strings and print the missing words in java

I want to make an application to find differences between two strings. How do I solve this?

st = "this is a cat.this is my cat."

st1 = "this is cat. this my cat."


The output should be "a is" as the missing words.

Here is my code

@SuppressLint("DefaultLocale")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String st="this is a cat. this is my cat.";
TextView tv1=(TextView) findViewById(R.id.textView2);

String st1="This is cat. this my cat.";

String blank="";
if(st.toLowerCase().contains(st1.toLowerCase()))
{
st=st.toLowerCase().replace(st1.toLowerCase(), blank);
tv1.setText(st);
}
}

Answer

You could use StringUtils.differences, it's the source:

6020        public static String difference(String str1, String str2) {
6021            if (str1 == null) {
6022                return str2;
6023            }
6024            if (str2 == null) {
6025                return str1;
6026            }
6027            int at = indexOfDifference(str1, str2);
6028            if (at == INDEX_NOT_FOUND) {
6029                return EMPTY;
6030            }
6031            return str2.substring(at);
6032        }
6033    
6034        /**
6035         * <p>Compares two Strings, and returns the index at which the
6036         * Strings begin to differ.</p>
6037         *
6038         * <p>For example,
6039         * <code>indexOfDifference("i am a machine", "i am a robot") -> 7</code></p>
6040         *
6041         * <pre>
6042         * StringUtils.indexOfDifference(null, null) = -1
6043         * StringUtils.indexOfDifference("", "") = -1
6044         * StringUtils.indexOfDifference("", "abc") = 0
6045         * StringUtils.indexOfDifference("abc", "") = 0
6046         * StringUtils.indexOfDifference("abc", "abc") = -1
6047         * StringUtils.indexOfDifference("ab", "abxyz") = 2
6048         * StringUtils.indexOfDifference("abcde", "abxyz") = 2
6049         * StringUtils.indexOfDifference("abcde", "xyz") = 0
6050         * </pre>
6051         *
6052         * @param str1  the first String, may be null
6053         * @param str2  the second String, may be null
6054         * @return the index where str2 and str1 begin to differ; -1 if they are equal
6055         * @since 2.0
6056         */
6057        public static int indexOfDifference(String str1, String str2) {
6058            if (str1 == str2) {
6059                return INDEX_NOT_FOUND;
6060            }
6061            if (str1 == null || str2 == null) {
6062                return 0;
6063            }
6064            int i;
6065            for (i = 0; i < str1.length() && i < str2.length(); ++i) {
6066                if (str1.charAt(i) != str2.charAt(i)) {
6067                    break;
6068                }
6069            }
6070            if (i < str2.length() || i < str1.length()) {
6071                return i;
6072            }
6073            return INDEX_NOT_FOUND;
6074        }
6075    
6076        /**
6077         * <p>Compares all Strings in an array and returns the index at which the
6078         * Strings begin to differ.</p>
6079         *
6080         * <p>For example,
6081         * <code>indexOfDifference(new String[] {"i am a machine", "i am a robot"}) -> 7</code></p>
6082         *
6083         * <pre>
6084         * StringUtils.indexOfDifference(null) = -1
6085         * StringUtils.indexOfDifference(new String[] {}) = -1
6086         * StringUtils.indexOfDifference(new String[] {"abc"}) = -1
6087         * StringUtils.indexOfDifference(new String[] {null, null}) = -1
6088         * StringUtils.indexOfDifference(new String[] {"", ""}) = -1
6089         * StringUtils.indexOfDifference(new String[] {"", null}) = 0
6090         * StringUtils.indexOfDifference(new String[] {"abc", null, null}) = 0
6091         * StringUtils.indexOfDifference(new String[] {null, null, "abc"}) = 0
6092         * StringUtils.indexOfDifference(new String[] {"", "abc"}) = 0
6093         * StringUtils.indexOfDifference(new String[] {"abc", ""}) = 0
6094         * StringUtils.indexOfDifference(new String[] {"abc", "abc"}) = -1
6095         * StringUtils.indexOfDifference(new String[] {"abc", "a"}) = 1
6096         * StringUtils.indexOfDifference(new String[] {"ab", "abxyz"}) = 2
6097         * StringUtils.indexOfDifference(new String[] {"abcde", "abxyz"}) = 2
6098         * StringUtils.indexOfDifference(new String[] {"abcde", "xyz"}) = 0
6099         * StringUtils.indexOfDifference(new String[] {"xyz", "abcde"}) = 0
6100         * StringUtils.indexOfDifference(new String[] {"i am a machine", "i am a robot"}) = 7
6101         * </pre>
6102         *
6103         * @param strs  array of strings, entries may be null
6104         * @return the index where the strings begin to differ; -1 if they are all equal
6105         * @since 2.4
6106         */
6107        public static int indexOfDifference(String[] strs) {
6108            if (strs == null || strs.length <= 1) {
6109                return INDEX_NOT_FOUND;
6110            }
6111            boolean anyStringNull = false;
6112            boolean allStringsNull = true;
6113            int arrayLen = strs.length;
6114            int shortestStrLen = Integer.MAX_VALUE;
6115            int longestStrLen = 0;
6116    
6117            // find the min and max string lengths; this avoids checking to make
6118            // sure we are not exceeding the length of the string each time through
6119            // the bottom loop.
6120            for (int i = 0; i < arrayLen; i++) {
6121                if (strs[i] == null) {
6122                    anyStringNull = true;
6123                    shortestStrLen = 0;
6124                } else {
6125                    allStringsNull = false;
6126                    shortestStrLen = Math.min(strs[i].length(), shortestStrLen);
6127                    longestStrLen = Math.max(strs[i].length(), longestStrLen);
6128                }
6129            }
6130    
6131            // handle lists containing all nulls or all empty strings
6132            if (allStringsNull || (longestStrLen == 0 && !anyStringNull)) {
6133                return INDEX_NOT_FOUND;
6134            }
6135    
6136            // handle lists containing some nulls or some empty strings
6137            if (shortestStrLen == 0) {
6138                return 0;
6139            }
6140    
6141            // find the position with the first difference across all strings
6142            int firstDiff = -1;
6143            for (int stringPos = 0; stringPos < shortestStrLen; stringPos++) {
6144                char comparisonChar = strs[0].charAt(stringPos);
6145                for (int arrayPos = 1; arrayPos < arrayLen; arrayPos++) {
6146                    if (strs[arrayPos].charAt(stringPos) != comparisonChar) {
6147                        firstDiff = stringPos;
6148                        break;
6149                    }
6150                }
6151                if (firstDiff != -1) {
6152                    break;
6153                }
6154            }
6155    
6156            if (firstDiff == -1 && shortestStrLen != longestStrLen) {
6157                // we compared all of the characters up to the length of the
6158                // shortest string and didn't find a match, but the string lengths
6159                // vary, so return the length of the shortest string.
6160                return shortestStrLen;
6161            }
6162            return firstDiff;
6163        }

then

difference("this is a cat.this is my cat.", "this is cat. this my cat.");

You could just implement this methods, or the entire library if you need more methods from this library.

Documentation here.

Comments