match two strings and print the missing words in j

2019-05-31 14:23发布

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);
    }
}

5条回答
The star\"
2楼-- · 2019-05-31 14:40

Сorrect solution is given below:

import java.util.ArrayList;

public class Classwithoutnewkeyword {
    public static void main(String args[]) {
        try {
            String  s1  = "this is a cat. this is my cat.";
            String  s2 =  "this is cat. my cat.";
            String arr1[] = s1.split(" ");
            String arr2[] = s2.split(" ");
            ArrayList<String> tmp1 = new ArrayList<String>();
            ArrayList<String> tmp2 = new ArrayList<String>();

            for (int i = 0,j=0; i < arr1.length; i++) {
                if (arr1[i].equalsIgnoreCase(arr2[j])) {
                    tmp1.add(arr1[i]);
                    j++;
                } else {
                    tmp2.add(arr1[i]);
                }
            }
            System.err.println(tmp2);

        } catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-05-31 14:58

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.

查看更多
狗以群分
4楼-- · 2019-05-31 15:01
Try this one.

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;


public class Classwithoutnewkeyword {
    public static void main(String args[]) {
        try {
                   String  s1  = "this is a cat.this is my cat.";
                   String  s2 = "this is cat. this my cat."
            String arr1[] = s1.split(" ");
            String arr2[] = s2.split(" ");
            java.util.List<String> list1 = new ArrayList<String>(
                    Arrays.asList(arr1));
            java.util.List<String> list2 = new ArrayList<String>(
                    Arrays.asList(arr2));

            ArrayList<String> tmp1 = new ArrayList<String>();
            ArrayList<String> tmp2 = new ArrayList<String>();

            for (int i = 0; i < arr1.length; i++) {
                int k = 0;
                for (int j = 0; j < arr2.length; j++) {

                    if (arr1[i].equalsIgnoreCase(arr2[j])) {
                        tmp1.add(arr1[i]);
                    } else {
                        tmp2.add(arr1[i]);
                    }

                }

            }
            for (String strs : tmp1) {
                list1.remove(strs);
            }

            System.out.print(list1.toString());

    }catch(Exception e)
    {
        e.printStackTrace();
    }
}
}
查看更多
Animai°情兽
5楼-- · 2019-05-31 15:02

The easy way is to split the both strings on the bases of spaces. e.g

String[] separated_st = st.split(" ");
String[] separated_st1 = st1.split(" ");

now you have two arrays. loop through them and find what is missing and what is not.

Alternatively you can use StringTokenizer class. Hope this will help.

查看更多
我想做一个坏孩纸
6楼-- · 2019-05-31 15:06
登录 后发表回答