【问题标题】:match two strings and print the missing words in java匹配两个字符串并在java中打印缺失的单词
【发布时间】:2014-03-31 07:05:03
【问题描述】:

我想制作一个应用程序来查找两个字符串之间的差异。我该如何解决?

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

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

输出应该是“a is”作为缺失的单词。

这是我的代码

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

【问题讨论】:

  • 但是“这是一只猫。这是我的猫。”不包含“这是猫。这是我的猫。”
  • 我明白但我不知道我用什么方法来解决这个问题
  • 好的,在下面查看我的答案
  • 好的,非常感谢
  • 你已经有 3 个答案了……你知道怎么做吗?

标签: android string string-matching


【解决方案1】:

【讨论】:

    【解决方案2】:

    你可以使用StringUtils.differences,它的来源:

    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        }
    

    然后

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

    如果你需要这个库中的更多方法,你可以只实现这个方法,或者整个库。

    文档here

    【讨论】:

      【解决方案3】:

      简单的方法是在空格的基础上拆分两个字符串。 例如

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

      现在你有两个数组。遍历它们,找出缺少的和没有的。

      您也可以使用 StringTokenizer 类。 希望这会有所帮助。

      【讨论】:

        【解决方案4】:
        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();
            }
        }
        }
        

        【讨论】:

        • 在您的示例中,字符串 s1 的每个单词都与整个字符串 s2 进行比较,我想检查句子的顺序
        • 所以你把字符串当作一个句子 ok va。然后应用 ma 逻辑,你肯定会得到你的解决方案。
        【解决方案5】:

        下面给出正确的解决方案:

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

        【讨论】:

          猜你喜欢
          • 2015-03-12
          • 2017-04-02
          • 2023-03-11
          • 1970-01-01
          • 2015-04-08
          • 2019-12-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多