简体   繁体   English

如何比较两个用于URL的字符串

[英]How do I compare two Strings being used for URLs

I am writing an application which looks at a file hosted on a server and if it changes it loads reloads a webview. 我正在编写一个应用程序,该应用程序查看服务器上托管的文件,如果更改,它将加载并重新加载Webview。 This is used to replicate a number of webpages across devices. 这用于在设备之间复制许多网页。

I set an initial url using: 我使用以下方法设置了初始网址:

private String originalURL;
private String newURL;

Then define the one to use on onCreate 然后定义一个在onCreate上使用

originalURL = "https://www.buzzfeed.com/sallytamarkin/total-body-workouts#.moYPngWVWy";

I then run an async task on a timer: 然后,我在计时器上运行异步任务:

private static String getUrlContents(String theUrl)
{
    StringBuilder content = new StringBuilder();

    // many of these calls can throw exceptions, so i've just
    // wrapped them all in one try/catch statement.
    try
    {
        // create a url object
        URL url = new URL(theUrl);

        // create a urlconnection object
        URLConnection urlConnection = url.openConnection();

        // wrap the urlconnection in a bufferedreader
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

        String line;

        // read from the urlconnection via the bufferedreader
        while ((line = bufferedReader.readLine()) != null)
        {
            content.append(line + "\n");
        }
        bufferedReader.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return content.toString();
}

private class BackgroundTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        // perform your network logic here
        String output  = getUrlContents("http://52.30.226.234/url.txt");
        System.out.println("output is " + output);
        newURL = output;
        return "YourResult";
    }

    @Override
    protected void onPostExecute(String result) {
        // Update your screen with the results.
    }
}

When I try to compare the two strings however they always evaluate as different. 但是,当我尝试比较两个字符串时,它们始终会得出不同的结果。 I have tried match(), equals() and equalsIgnoreCase(). 我已经尝试过match(),equals()和equalsIgnoreCase()。 This is the code I am using 这是我正在使用的代码

if ((originalURL.equals(newURL))==true) {
                System.out.println("SAME");
            } else {
                System.out.println("NOT SAME");
            }
        }

I have also tested this with two strings defined earlier and it works fine. 我还使用前面定义的两个字符串对此进行了测试,并且效果很好。

I can only think this is because of special characters but have tried several fixes to remove special characters and this has not worked either. 我只能认为这是由于特殊字符引起的,但是已经尝试了一些修复程序来删除特殊字符,但这也没有用。

Please help! 请帮忙!

Don't write "true" in your if-clause ; 不要在if-clause写“ true”;

Do this if you want Case-Sensitive : 如果要Case-Sensitive执行以下操作:

if(string1.equals(string2)){
    System.out.println("SAME");
 } 
 else{
    System.out.println("NOT SAME");              
}

Do this if you want Case-Insensitive : 如果您希望Case-Insensitive执行以下操作:

if(string1.equalsIgnoreCase(string2)){
    System.out.println("SAME");
 } 
 else{
    System.out.println("NOT SAME");              
}

If it doesn't fit what you want, or you wanna make a more rigorous comparison try using Collator 如果它不符合您的要求,或者您想进行更严格的比较,请尝试使用整理器

Probably due to the gibberish part of the url at the end. 可能是由于网址末尾出现乱码。 This part changes often. 这部分经常更改。 Print to see whether they are same. 打印以查看它们是否相同。

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

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