Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较C语言中的字符串#_C#_String_Compare - Fatal编程技术网

C# 比较C语言中的字符串#

C# 比较C语言中的字符串#,c#,string,compare,C#,String,Compare,好的,我试着每15秒比较两个字符串,然后更新一个信息框 以下是我到目前为止从web获取文本文档并将其存储到字符串中的代码: public String GetData(String url) { WebRequest request = WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response

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

以下是我到目前为止从web获取文本文档并将其存储到字符串中的代码:

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);
    }
}
public void CompareStrings()
{
int x;
x=1;
字符串数据=GetData(“http://xcastradio.com/stats/nowplaying.txt");
字符串savedData=数据;
而(x>0&&x<10000001)
{
x++;
}
字符串data1=GetData(“http://xcastradio.com/stats/nowplaying.txt");
nowplayingfo1.Text=数据;
NowPlaying np=新的NowPlaying();
如果(数据1!=savedData)
{
NowPlayingInfo1.Text=data1;
np.Show(这个);
}
}
这个检查正在播放的歌曲的线程应该与主应用程序线程分开,因为它处于休眠状态,并且(我认为)你希望你的应用程序即使在两次检查之间也能保持响应

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

这个检查正在播放的歌曲的线程应该与主应用程序线程分开,因为它处于休眠状态,并且(我认为)你希望你的应用程序即使在两次检查之间也能保持响应


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

我不是故意小题大做,但其目的是什么:

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

如果需要暂停,为什么不直接使用Thread.Sleep(TimeSpan.FromSeconds(1))呢?您的代码示例没有太多意义

我并不是故意小题大做,但这样做的目的是什么:

    while (x > 0 && x < 100000001)
    {
        x++;
    }
while(x>0&&x<10000001)
{
x++;
}
如果需要暂停,为什么不直接使用Thread.Sleep(TimeSpan.FromSeconds(1))呢?您的代码示例没有太多意义

String.Compare(string1、string2……)
提供了更多选项

请参阅MSDN上的字符串。比较(字符串1、字符串2……)提供了更多选项


请参阅MSDN上的

我认为您的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();
    }
}
此代码使用
计时器
每秒检查一次URL。每当此文本文件的内容发生更改时,此代码将弹出一个新的
NowPlaying
窗口(我认为您正在尝试这样做),并将继续执行此操作,直到您将
\u比较
设置为
false


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

我认为您的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();
    }
}
此代码使用
计时器
每秒检查一次URL。每当此文本文件的内容发生更改时,此代码将弹出一个新的
NowPlaying
窗口(我认为您正在尝试这样做),并将继续执行此操作,直到您将
\u比较
设置为
false


您还可能希望轮询URL的频率低于每秒一次,在这种情况下,您可以将
计时器.Interval
设置为大约10000(10秒)。

我建议您改为使用以下方法:

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

  • 我建议您采用以下方法:

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

  • 你的问题是什么?…但还有很多其他问题等着问;)“while(x>0&&x<100000000001)”我们又回到了80年代……啊,让他休息一下,他正在学习。西斯托伦:
    Thread.Sleep()
    将是一种方法。事实上,编译器可能认识到您的计时循环没有任何作用,只需将
    x
    设置为100000000001即可对其进行优化和替换。您的问题是什么?…但还有许多其他问题有待解决;)“while(x>0&&x<100000000001)”我们又回到了80年代……啊,让他休息一下,他正在学习。西斯托伦:
    Thread.Sleep()
    将是一种方法。事实上,编译器可能会意识到您的计时循环没有任何作用,并对其进行优化,只需将
    x
    设置为10000001即可替换它。@David:确切地说,虽然与字符串比较没有任何关系,但循环到“delay”是开发人员的“不允许”之一。@okw:using
    Thread.Sleep(n)
    延迟几乎和循环一样重要(尽管它至少不会产生依赖处理器的持续时间)。当时我处于脱机状态,无法找到一种方法让它每隔一段时间停止。因此,让我的计算机计数只是为了看看我写的东西是否能按我当时希望的方式工作。@Musi:使用睡眠很好,只要你在后台线程中进行。@David:没错,虽然与字符串比较无关,但循环到“延迟”是开发人员的“不允许”之一。@okw:使用