C# 中的比较字符串

发布于 2024-08-11 18:06:56 字数 1015 浏览 1 评论 0原文

好的,我尝试每 15 秒比较两个字符串,然后更新信息框。

这是迄今为止我从网络获取文本文档并将其存储到字符串中的代码:

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;
}

这是我尝试比较字符串的代码。

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);
    }
}

Ok,I am trying to compare two strings every 15 seconds and then update an info box.

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

策马西风 2024-08-18 18:06:56

我并不是故意尖酸刻薄,但这样做的目的是什么:

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

如果你想要暂停,为什么不只是 Thread.Sleep(TimeSpan.FromSeconds(1)) 呢?您的代码示例没有太大意义。

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))? Your code sample doesn't make too much sense.

流云如水 2024-08-18 18:06:56

String.Compare(string1, string2 ......) 为您提供更多选择。

请参阅 MSDN 上的 String.Compare 方法

String.Compare(string1, string2 ......) gives you more options.

Refer to String.Compare Method on MSDN

指尖上得阳光 2024-08-18 18:06:56

我认为您的 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();
    }
}

此代码使用 Timer 每秒检查一次 URL。每当此文本文件的内容发生更改时,此代码都会弹出一个新的 NowPlaying 窗口(这就是我认为您正在尝试做的事情),并将继续执行此操作,直到您设置 >_比较

您还可能希望轮询 URL 的频率低于每秒​​一次,在这种情况下,您可以将 timer.Interval 设置为 10000(10 秒)之类的值。

I think your CompareStrings() method should be something like this:

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. 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.

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).

泪是无色的血 2024-08-18 18:06:56
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);
        }

    }

用于检查正在播放的歌曲的线程应该与主应用程序线程分开,因为它会休眠,并且您希望(我认为)您的应用程序即使在检查之间也能保持响应。

编辑:比较现在应该可以正常工作(未经测试)。

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).

爱*していゐ 2024-08-18 18:06:56

我建议您使用以下方法:

  1. 生成已保存数据的哈希并存储值,如果您不需要真正...则不需要创建大型字符串对象...
  2. 对于所有新读取,只需生成一个哈希并将其与保存的哈希对象进行比较。
  3. 使用任何哈希算法,但我建议使用 shah1。
  4. 使用 String 类内置的 String.Compare(...) 方法来比较哈希对象。
  5. 尝试 Thread.Sleep([millisecondsvaluehere]) 暂停程序执行。您还可以考虑将读取调用放入计时器、表单或系统计时器中(确保在访问 UI 对象之前调用)

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.
  4. Use the built in String.Compare(...) method of the String class to compare the hash objects.
  5. Try Thread.Sleep([millisecondsvaluehere]) to pause program execution. You could also consider putting the read call in a timer, forms or system timer (make sure to invoke before accessing UI objects)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文