简体   繁体   中英

How can i compare the last 10 characters of two given Strings?

I am a beginner to the programming and I have given a try with the following code

package Logics;

public class String1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String1 str=new String1();
    str.String12("My name is harish","My name is Soundarya");

}

public void String12(String a, String b) {

    int count1=0;
    int count2=0;
    for(int i=a.length()-1;i>=0;i--){

        count1=count1+1;
    char s1=a.charAt(i);
    //char s2=b.charAt(i);

    while(count1<=10){

        System.out.println("The last 10 characters of given string 1 are " +s1);
        break;
    }

    for(int j=b.length()-1;j>=0;j++)
    {
        count2=count2+1;
        char s2=b.charAt(j);
        while(count2<=10) {

            System.out.println("The last characters of given String 2 are " +s2);
        }
    }

}
}

}

But my code is not entering the second for loop. Can anyone please help?

looking at your code, I notice that 2nd for loop is not correct change for loop as below:

for(int j=b.length()-1;j>=0;j--)

Also, use break; in 2nd while loop; otherwise, you will get infinite loop there!

PS: However, this is not the good approach; you may refer to Dennis's answer for better approach.

I would use substring to compare a number of characters

(a.substring(a.length-10).equals(b.substring(b.length-10))

substring returns a part of the original String beginnning with the passed index (here a.length-10) and equals compares them

Your Code is not entering the second loop because j is set to b.length()-1, a positive value, but the loop is only executing when j is lower or equal 0.

Note that you have an endless while-loop inside the second for-loop. You should change the value off count2 inside this loop.

Also note that your first while loop is only executed once, since the break ends it. You could simply remove the loop and you'd have the same result

You can use a StringBuffer or a StringBuilder to do this.

int len = a.length();
StringBuffer sb1 = new StringBuffer(a);
System.out.println("The last 10 characters of given string 1 are " +sb1.substring(len-10,len));

This will easily work.

package package_One;

public class StringHandling {

    //compare only last 10 characters of two string
    String s1 = "This is Testing World";
    String s2 = "My Testingf World";

    public void stringHandling(int num)
    {
        if(s1.substring(s1.length()-num, s1.length()).equals(s2.substring(s2.length()-num, s2.length())))
        {
            System.out.println("Both substrings are equal");
        }
        else 
        {
            System.out.println("Both substrings are not equal");
        }
    }
}

-- package package_One;

public class StringMain {

    public static void main(String[] args) {

    StringHandling obj = new StringHandling();
            obj.stringHandling(10);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM