c# - 在 C# 中比较字符串

标签 c# string compare

好的,我尝试每 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);
    }
}

最佳答案

我并不是要刻薄,但目的是什么:

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

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

关于c# - 在 C# 中比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1753184/

相关文章:

c# - 如何在 C# 中的警告框确定按钮上编写操作?

c# - 为什么我不能在调试器中编辑包含匿名方法的方法?

java - 将数组转换为不带括号、逗号或空格的字符串

Java Swing绘制html格式的字符串

逐个字符比较 2 个字符串

PHP:检查 DateTime 是否未过期

android - 将空字段与字符串进行比较?

c# - 从 C# 批量数据库插入的最佳方法是什么?

c# - C# 和 C++ 中虚拟函数的编译器行为差异。这是我在采访中被问到的

php - 在没有mysqli_real_escape_string的情况下在数据库中用单引号保存字符串