比较Java中的字符串,并删除字符串相同的部分 [英] Compare strings in java and remove the part of string where they are identical

查看:677
本文介绍了比较Java中的字符串,并删除字符串相同的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串:

s1="MICROSOFT"
s2="APPLESOFT"

我需要比较字符串并从第二个字符串中删除重复的部分(总是朝着结尾)。因此,我应该得到 MICROSOFT和 APPLE作为输出。

I need to compare the strings and remove the duplicate part (always towards the end) from the second string. So I should get "MICROSOFT" and "APPLE" as output.

我已经逐个字符地比较了两个字符串。

I have compared both the strings character by character.

               String s1 = "MICROSOFT";
               String s2 = "APPLESOFT";

               for(int j=0; j<s1.length(); j++)
               {
                   char c1 = s1.charAt(j);
                   char c2 = s2.charAt(j);

                   if(c1==c2)
                       System.out.println("Match found!!!");
                   else
                       System.out.println("No match found!");
               }

它应该检查字符串,以及两个字符串是否具有相同的字符,直到结尾字符串,那么我需要从第二个字符串中删除该冗余部分,在这种情况下为SOFT。但我想不出如何从这里开始。

It should check the strings and if the two strings have same characters until the end of string, then I need to remove that redundant part, SOFT in this case, from the second string. But I can't think of how to proceed from here.

可以有更多重复项,但是我们只需要删除连续重复的重复项即可。如果我有APPWWSOFT和APPLESOFT,我应该在第二个字符串中再次获得APPLE,因为我们在这两者之间获得的LE与WW不同

There can be more duplicates...but we have to remove only those which are continuously identical. if i have APPWWSOFT and APPLESOFT, i should get APPLE again in the second string since we got LE different than WW in between

你们能帮我吗?

推荐答案

在解决问题后,我解决了我的问题。请随时更正/改进/完善我的代码。该代码不仅适用于 MICROSOFT和 APPLESOFT输入,还适用于 APPWWSOFT和 APPLESOFT之类的输入(我需要从头删除连续的重复项-在上述两个输入中均为SOFT)。我正处于学习阶段,我将不胜感激。

I have solved my problem after racking some brains off. Please feel free to correct/improve/refine my code. The code not only works for "MICROSOFT" and "APPLESOFT" inputs, but also for inputs like "APPWWSOFT" and "APPLESOFT" (i needed to remove the continuous duplicates from the end - SOFT in both the above inputs). I'm in the learning stage and I'll appreciate any valuable inputs.

public class test
    {           
        public static void main(String[] args)
        {
            String s1 = "MICROSOFT";
            String s2 = "APPLESOFT";

            int counter1=0;
            int counter2=0;

            String[] test = new String[100];
            test[0]="";

            for(int j=0; j<s1.length(); j++)
            {
                char c1 = s1.charAt(j);
                char c2 = s2.charAt(j);

                if(c1==c2)
                {
                    if(counter1==counter2)
                    {
                        //System.out.println("Match found!!!");
                        test[0]=test[0]+c2;
                        counter2++;
                        //System.out.println("Counter 2: "+counter2);
                    }
                    else
                        test[0]="";
                }
               else
               {
                   //System.out.print("No match found!");
                   //System.out.println("Counter 2: "+counter2);
                   counter2=counter1+1;
                   test[0]="";
               }

               counter1++;
               //System.out.println("Counter 1: "+counter1);
                           }

             System.out.println(test[0]);
             System.out.println(s2.replaceAll(test[0]," "));
        }
    }

这篇关于比较Java中的字符串,并删除字符串相同的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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