【问题标题】:C# Find specific text in listboxC#在列表框中查找特定文本
【发布时间】:2015-07-25 12:46:10
【问题描述】:

我正在创建一个应用程序,该应用程序在后台运行 Java 服务器,并在每个新行中输出到控制台列表框。但是,我希望能够创建一个计时器,例如,不断读取控制台列表框以获取某些预先确定的内容,但不具体。

例如,如果服务器输出“[17:32:50 INFO]: Matt[127.0.0.1]logged in with entity ID 233 at ([world]co-ordinates in world)”并将其添加到控制台列表框作为一个项目,我希望能够抓住“马特”部分并将其添加到玩家列表框中当前在线的玩家列表中。 “实体ID”和“世界坐标”每次都不一样,无法确定。

除此之外,我还想在玩家断开连接时做同样的事情,控制台列表框会添加一个项目,上面写着“马特离开游戏”,我需要它才能搜索玩家“Matt”的列表框并删除该项目。

我想知道这是否可能,如果这不是很清楚,请道歉!

【问题讨论】:

  • 你能给我们举个你试过的例子吗?编辑你失败的尝试比猜测你的想法更容易。
  • 我没试过。我不知道我怎么可能做到这一点。我能想到的唯一方法可能是在“[”字符之前获取所有文本。对此深表歉意。

标签: c# string text listbox


【解决方案1】:

编辑:

如果您想获取玩家姓名,然后在“在线玩家”列表框中添加或删除它,您可以使用Substring() 在从 x 索引开始并选择 y 长度的字符串中进行搜索。

这里有一个更新版本,应该可以使用新格式:

private void cmdLogin_Click(object sender, EventArgs e)
{
    // Add example logins
    lstConsoleOutput.Items.Add("[17:32:50 INFO]: Amy[/127.0.0.1:12345]logged in with entity ID 233 at ([world]co-ordinates in world)");
    lstConsoleOutput.Items.Add("[18:42:51 INFO]: Dan[/255.255.255.255:23451]logged in with entity ID 233 at ([world]co-ordinates in world)");
    lstConsoleOutput.Items.Add("[19:33:27 INFO]: Matt[/1.1.1.1:34512]logged in with entity ID 233 at ([world]co-ordinates in world)");
    lstConsoleOutput.Items.Add("[02:17:03 INFO]: Some Really Long Screen Name[/292.192.92.2:45123]logged in with entity ID 233 at ([world]co-ordinates in world)");
    lstConsoleOutput.Items.Add("[09:05:55 INFO]: ABC $!@#$ 12341234[/127.0.0.1:51234]logged in with entity ID 233 at ([world]co-ordinates in world)");

    AnalyzeConsoleOutput(lstConsoleOutput, lstOnlinePlayers);
}

private void cmdLogout_Click(object sender, EventArgs e)
{
    // Add example logouts
    lstConsoleOutput.Items.Add("[22:12:12 INFO]: Amy left the game");
    lstConsoleOutput.Items.Add("[23:44:15 INFO]: Matt left the game");
    lstConsoleOutput.Items.Add("[24:00:00 INFO]: ABC $!@#$ 12341234 left the game");

    AnalyzeConsoleOutput(lstConsoleOutput, lstOnlinePlayers);
}

private void AnalyzeConsoleOutput(ListBox listboxToAnalyze, ListBox onlinePlayersListbox)
{
    const string LoginIdentifier = "]logged in with entity";
    const string LogoutIdentifer = " left the game";
    string playerName;

    foreach (var item in listboxToAnalyze.Items)
    {
        // Check if a player has logged in and add them to the players list box
        if (item.ToString().Contains(LoginIdentifier))
        {
            int startOfPlayerNameIndex = item.ToString().IndexOf("]: ") + 3;
            int lengthOfPlayerName = item.ToString().IndexOf('[', item.ToString().IndexOf('[') + 1) - startOfPlayerNameIndex;

            playerName = item.ToString().Substring(startOfPlayerNameIndex, lengthOfPlayerName);

            if (!onlinePlayersListbox.Items.Contains(playerName))
            {
                onlinePlayersListbox.Items.Add(playerName);
            }
        }

        // Check if a player has logged out and remove them from the players list box
        if (item.ToString().Contains(LogoutIdentifer))
        {
            int startOfPlayerNameIndex = item.ToString().IndexOf("]: ") + 3;
            int lengthOfPlayerName = item.ToString().IndexOf(LogoutIdentifer, 0) - startOfPlayerNameIndex;

            playerName = item.ToString().Substring(startOfPlayerNameIndex, lengthOfPlayerName);

            if (onlinePlayersListbox.Items.Contains(playerName))
            {
                onlinePlayersListbox.Items.Remove(playerName);
            }
        }
    }
}

这现在使用 2 个变量来设置玩家姓名的开始索引,并使用另一个变量来设置要计数的字符数。通过这种方式,如果将来格式发生变化,您只需要更新这些内容,其他一切都应该仍然有效。我还保留了 LoginIdentifier 和 LogoutIdentifier 的 2 个常量,您可以根据需要进行更改。

无论是否在 IP 地址中使用 /,这也应该适用。所以“[111”或“[/111”应该仍然可以正常工作。希望这会有所帮助。

【讨论】:

  • 对不起,我错过了字符串的开头。我现在看看那个。只是想知道,您是否需要知道“实体 id”或“coordinatesinworld”是什么?因为如果不是,您可以将 LoginIdentifier 更改为类似“]logged in with entity id”。
  • 是的,你是对的。但是,您可以更新代码以搜索您知道将始终相同的部分,例如 "]logged in with entity id" 或 "INFO]: "。我用一些应该适用于这种格式的新代码为你更新了我的答案。希望对您有所帮助。
  • 我也很抱歉,但我刚刚意识到您的 cmets 将其写为 Matt[/127,但您编辑的问题将其写为 Matt[127。你能澄清一下它是什么格式吗?我更新的代码适用于 [/127,如果它只是 [127.
  • 哦,没关系。我想我知道现在删除的问题是什么。是否所有的“名字已经离开游戏”在他们面前都有一些东西,比如 [xx:xx:xx INFO]: Matt has leave the game?
  • 没问题,不客气。我刚刚用一个更新的示例再次更新了我的答案,该示例应该适用于您添加和删除玩家的新格式。
【解决方案2】:
foreach (var listBoxItem in listBox1.Items)
{
    // use the currently iterated list box item

    if (listBoxItem.ToString().Contains("logged"))
    {
        // Do your stuff here
    }
}

【讨论】:

  • 您好,这非常适合查找正确的项目!但是,不能帮助我从该项目中收集某些文本,感谢您的帮助!
【解决方案3】:

您可以遍历列表框的项目并搜索拆分的对象并选择拆分的“左侧”。

string CheckForMatchNames()
{
    foreach(string item in listbox.items)
        for (int i = 0; i < preDeterminedStringArrayThatMustMatchSomething.Length; i++)
            if (item.Split('[')[0] == preDeterminedStringArrayThatMatchesSomething[i])
                return preDeterminedStringArrayThatMustMatchSomething[i];
    return "none";
}

【讨论】:

  • 你好,我正在努力让这段代码正常工作,但我已经输入了它需要的信息,但仍然有错误。另外,我不确定这是否会得到字符串的“Matt”部分?
  • 它会用于登录,而不是用于注销,但您已经得到了答案。我试过了。
猜你喜欢
  • 2016-06-11
  • 2020-05-13
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多