简体   繁体   English

比较C#中的字符串

[英]Compare Strings in C#

Ok,I am trying to compare two strings every 15 seconds and then update an info box. 好的,我尝试每15秒比较两个字符串,然后更新一个信息框。

Here is the code I have so far to get a text document from the web and store it into a string: 到目前为止,这是我从网上获取文本文档并将其存储为字符串的代码:

public String GetData(String url)
{
    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    String data = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    response.Close();
    return data;
}

And here is what I have with trying to compare the strings. 这就是我尝试比较字符串的方法。

public void CompareStrings()
{
    int x;
    x = 1;
    String data = GetData("http://xcastradio.com/stats/nowplaying.txt");
    string savedData = data;
    while (x > 0 && x < 100000001)
    {
        x++;
    }
    String data1 = GetData("http://xcastradio.com/stats/nowplaying.txt");
    NowPlayingInfo1.Text = data;
    NowPlaying np = new NowPlaying();
    if (data1 != savedData)
    {
        NowPlayingInfo1.Text = data1;
        np.Show(this);
    }
}

I don't mean to be snarky but what is the purpose of: 我的意思不是嘴,但目的是:

    while (x > 0 && x < 100000001)
    {
        x++;
    }

If you want a pause, why not just Thread.Sleep(TimeSpan.FromSeconds(1))? 如果要暂停,为什么不只是Thread.Sleep(TimeSpan.FromSeconds(1))? Your code sample doesn't make too much sense. 您的代码示例没有太大意义。

String.Compare(string1, string2 ......) gives you more options. String.Compare(string1, string2 ......)给您更多选择。

Refer to String.Compare Method on MSDN 请参考MSDN上的String.Compare方法

I think your CompareStrings() method should be something like this: 我认为您的CompareStrings()方法应如下所示:

private bool _Comparing = false;
private string _URL = "http://xcastradio.com/stats/nowplaying.txt";
private string _data = "";
public void CompareStrings()
{
    Timer timer = new Timer();
    timer.Interval = 1000;
    timer.Tick += timer_Tick;
    _data = GetData(_URL);
    _Comparing = true;
    timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
    if (_Comparing)
    {
        string newdata = GetData(_URL);
        if (newdata != _data)
        {
            NowPlaying np = new NowPlaying();
            NowPlayingInfo1.Text = newdata;
            _data = newdata;
            np.Show(this);
        }
    }
    else
    {
        Timer timer = (Timer)sender;
        timer.Stop();
    }
}

This code uses a Timer to check the URL once every second. 此代码使用Timer每秒检查一次URL。 Whenever the contents of this text file changes, this code will pop up a new NowPlaying window (which is what I think you're trying to do), and will continue to do this until you set _Comparing to false . 每当此文本文件的内容更改时,此代码都会弹出一个新的NowPlaying窗口(这是我认为您要尝试的操作),并将继续执行此操作,直到将_Comparing设置为false为止。

You also might want to poll the URL less frequently than once per second, in which case you would set timer.Interval to something like 10000 (10 seconds). 您可能还希望轮询URL的频率小于每秒一次,在这种情况下,您可以将timer.Interval设置为10000(10秒)之类的值。

public void CompareStrings()
    {
        String data = GetData("http://xcastradio.com/stats/nowplaying.txt");

        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15));

        String data1 = GetData("http://xcastradio.com/stats/nowplaying.txt");

        NowPlayingInfo1.Text = data;

        NowPlaying np = new NowPlaying();

        if (String.Compare(data, data1) != 0)
        {
            NowPlayingInfo1.Text = data1;
            np.Show(this);
        }

    }

This thread to check the song now playing should be separate from the main application thread, since it sleeps and you want (I think) for your app to keep responding even between checks. 用于检查正在播放的歌曲的线程应与主应用程序线程分开,因为它处于休眠状态,并且您希望(我认为)您的应用程序即使在两次检查之间也要保持响应。

Edit: Compare should now work correctly (not tested). 编辑:比较现在应该正确工作(未经测试)。

I recommend you instead, utilize the following: 我建议您改用以下内容:

  1. Generate a hash of the saved data and store the value, you don't need to create large string objects if you don't need to really... 生成保存的数据的哈希值并存储值,如果不需要,则不需要创建大型字符串对象。
  2. For all new reads, simply generate a hash and compare that against the saved hash object. 对于所有新读取,只需生成一个哈希并将其与保存的哈希对象进行比较即可。
  3. Use any hashing algorithm, but I would recommend using shah1. 使用任何哈希算法,但我建议使用shah1。
  4. Use the built in String.Compare(...) method of the String class to compare the hash objects. 使用String类的内置String.Compare(...)方法比较哈希对象。
  5. Try Thread.Sleep([millisecondsvaluehere]) to pause program execution. 尝试使用Thread.Sleep([millisecondsvaluehere])暂停程序执行。 You could also consider putting the read call in a timer, forms or system timer (make sure to invoke before accessing UI objects) 您也可以考虑将read调用放在计时器,表单或系统计时器中(确保在访问UI对象之前先调用)

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

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