匹配两个字符串,并打印在java中所​​缺单词 [英] match two strings and print the missing words in java

查看:145
本文介绍了匹配两个字符串,并打印在java中所​​缺单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要提出申请,找到两个字符串之间的差异。我该如何解决这个问题?

  ST =这是一个cat.this是我的猫。ST1 =这是猫。这是我的猫。

输出应该是是为所缺单词。

下面是我的code

  @燮pressLint(DefaultLocale)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    串ST =这是一只猫,这是我的猫。
    TextView的TV1 =(的TextView)findViewById(R.id.textView2);    字符串ST1 =这是这个猫我的猫。    字符串空白=;
    如果(st.toLowerCase()。含有(st1.toLowerCase()))
    {
        ST = st.toLowerCase()取代(st1.toLowerCase(),空白)。
        tv1.setText(ST);
    }
}


解决方案

您可以使用<一个href=\"http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/src-html/org/apache/commons/lang/StringUtils.html#line.6020\"相对=nofollow> StringUtils.differences 的,它的源代码:

  6020公共静态字符串差异(STR1字符串,字符串STR2){
6021如果(STR1 == NULL){
6022回STR2;
6023}
6024如果(STR2 == NULL){
6025 STR1回报;
6026}
6027 INT在= indexOfDifference(STR1,STR2);
6028如果(在== INDEX_NOT_FOUND){
6029空车返回;
6030}
6031回str2.substring(AT);
6032}
6033
6034 / **
6035 *&LT; P&GT;比较两个字符串,返回的索引,该
6036 *字符串开始有所不同 - LT; / P&GT;
6037 *
6038 *下; P&gt;作为例子,
6039 *&LT; code基indexOfDifference(我是一台,我是一个机器人) - GT; 7 LT; / code&GT;&LT; / P&GT;
6040 *
6041 *&LT; pre&GT;
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 *&LT; / pre&GT;
6051 *
6052 *参数STR1第一个字符串,可以为null
6053 *参数STR2第二个字符串,可以为null
6054 * @返回,其中STR2赛车,并且str1开始不同的索引; -1,如果它们相等
6055 * @since 2.0
6056 * /
6057公共静态INT indexOfDifference(字符串STR1,字符串STR2){
6058如果(STR1 == STR2){
6059回INDEX_NOT_FOUND;
6060}
如果6061(STR1 == NULL || STR2 == NULL){
6062返回0;
6063}
6064 INT I;
6065为(I = 0; I&下; str1.length()及&放大器; I&下; str2.length(); ++ⅰ){
6066如果(str1.charAt(ⅰ)!= str2.charAt(ⅰ)){
6067突破;
6068}
6069}
6070如果(ⅰ&下; str2.length()|| I&下; str1.length()){
6071回报我;
6072}
6073回INDEX_NOT_FOUND;
6074}
6075
6076 / **
6077 *&LT; P&GT;比较数组中的所有字符串,返回的索引,该
6078 *字符串开始有所不同 - LT; / P&GT;
6079 *
6080 *下; P&gt;作为例子,
6081 *&LT; code基indexOfDifference(新的String [] {我是一个机器,我是机器人}) - GT; 7 LT; / code&GT;&LT; / P&GT;
6082 *
6083 *&LT; pre&GT;
6084 * StringUtils.indexOfDifference(空)= - 1
6085 * StringUtils.indexOfDifference(新的String [] {})= -1
6086 * StringUtils.indexOfDifference(新的String [] {ABC})= -1
6087 * StringUtils.indexOfDifference(新的String [] {NULL,NULL})= -1
6088 * StringUtils.indexOfDifference(新的String [] {,})= -1
6089 * StringUtils.indexOfDifference(新的String [] {,NULL})= 0
6090 * StringUtils.indexOfDifference(新的String [] {ABC,NULL,NULL})= 0
6091 * StringUtils.indexOfDifference(新的String [] {NULL,NULL,ABC})= 0
6092 * StringUtils.indexOfDifference(新的String [] {,ABC})= 0
6093 * StringUtils.indexOfDifference(新的String [] {ABC,})= 0
6094 * StringUtils.indexOfDifference(新的String [] {ABC,ABC})= -1
6095 * StringUtils.indexOfDifference(新的String [] {ABC,一})= 1
6096 * StringUtils.indexOfDifference(新的String [] {AB,abxyz})= 2
6097 * StringUtils.indexOfDifference(新的String [] {ABCDE,abxyz})= 2
6098 * StringUtils.indexOfDifference(新的String [] {ABCDE,XYZ})= 0
6099 * StringUtils.indexOfDifference(新的String [] {XYZ,ABCDE})= 0
6100 * StringUtils.indexOfDifference(新的String [] {我是一个机器,我是机器人})= 7
6101 *&LT; / pre&GT;
6102 *
6103 * @参数可疑交易报告字符串数组,参赛作品可以为null
6104返回:那里的字符串开始不同的索引; -1,如果他们都是平等的
6105 * @since 2.4
6106 * /
6107公共静态INT indexOfDifference(字符串[]可疑交易报告){
6108如果序列(STR == NULL || strs.length&LT; = 1){
6109回INDEX_NOT_FOUND;
6110}
6111布尔anyStringNull = FALSE;
6112布尔allStringsNull = TRUE;
6113 INT arrayLen = strs.length;
6114 INT shortestStrLen = Integer.MAX_VALUE的;
6115 INT longestStrLen = 0;
6116
6117 //找到最小和最大字符串长度;这避免了检查,以
6118 //确保我们不会超过每次通过字符串的长度
6119 //底部循环。
6120(INT I = 0; I&LT; arrayLen;我++){
6121如果序列(STR [I] == NULL){
6122 anyStringNull = TRUE;
6123 shortestStrLen = 0;
6124}其他{
6125 allStringsNull = FALSE;
6126 shortestStrLen = Math.min序列(STR [I]。长度(),shortestStrLen);
6127 longestStrLen = Math.max(可疑交易报告[I]。长度(),longestStrLen);
6128}
6129}
6130
包含所有空6131 //处理列表或全部空字符串
如果6132(allStringsNull ||(longestStrLen == 0安培;&安培;!anyStringNull)){
6133回INDEX_NOT_FOUND;
6134}
6135
包含一些空值6136 //手柄名单还是有些空字符串
如果6137(shortestStrLen == 0){
6138返回0;
6139}
6140
6141 //找到所有字符串的第一个差的位置
6142 INT firstDiff = -1;
6143为(中间体stringPos = 0; stringPos&下; shortestStrLen; stringPos ++){
6144字符comparisonChar =可疑交易报告[0] .charAt(stringPos);
6145为(中间体arrayPos = 1; arrayPos&下; arrayLen; arrayPos ++){
6146如果序列(STR [arrayPos] .charAt(stringPos)!= comparisonChar){
6147 firstDiff = stringPos;
6148突破;
6149}
6150}
6151如果(firstDiff!= -1){
6152突破;
6153}
6154}
6155
如果6156(firstDiff == -1放大器;&安培; shortestStrLen = longestStrLen!){
6157 //我们比较所有的字符最多的长度
6158 //最短的字符串,并没有找到一个匹配,但字符串长度
6159 //有所不同,所以返回的最短串的长度。
6160回shortestStrLen;
6161}
6162回firstDiff;
6163}

然后

 差异(这是一个cat.this是我的猫。,这是这个猫我的猫。);

如果你需要从这个库的详细方法,你可能只是实现这个方法,或整个库。

文件<一个href=\"http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#difference%28java.lang.String,%20java.lang.String%29\"相对=nofollow>这里。

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

解决方案

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.

这篇关于匹配两个字符串,并打印在java中所​​缺单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆