简体   繁体   English

如何比较两个给定字符串的最后 10 个字符?

[英]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 {公共类 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.但是我的代码没有进入第二个for循环。 Can anyone please help?有人可以帮忙吗?

looking at your code, I notice that 2nd for loop is not correct change for loop as below:查看您的代码,我注意到第二个 for 循环不是正确的循环更改,如下所示:

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

Also, use break;另外,使用break; in 2nd while loop;在第二个 while 循环中; otherwise, you will get infinite loop there!否则,你会在那里得到无限循环!

PS: However, this is not the good approach; PS:但是,这不是好方法; you may refer to Dennis's answer for better approach.您可以参考丹尼斯的回答以获得更好的方法。

I would use substring to compare a number of characters我会使用 substring 来比较多个字符

(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 substring 返回原始字符串的一部分,以传递的索引(此处为 a.length-10)开头,并比较它们

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.您的代码没有进入第二个循环,因为 j 设置为 b.length()-1,一个正值,但循环仅在 j 小于或等于 0 时执行。

Note that you have an endless while-loop inside the second for-loop.请注意,您在第二个 for 循环中有一个无限的 while 循环。 You should change the value off count2 inside this loop.您应该在此循环内更改 count2 的值。

Also note that your first while loop is only executed once, since the break ends it.另请注意,您的第一个 while 循环仅执行一次,因为 break 结束了它。 You could simply remove the loop and you'd have the same result你可以简单地删除循环,你会得到相同的结果

You can use a StringBuffer or a StringBuilder to do this.您可以使用 StringBuffer 或 StringBuilder 来执行此操作。

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; -- 包 package_One;

public class StringMain {

    public static void main(String[] args) {

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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