C# 在列表框中查找特定文本

C# Find specific text in listbox

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

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

除此之外,我还想在玩家断开连接时做同样的事情,控制台列表框将添加一个显示 "Matt left the game" 的项目,我需要它才能搜索"Matt" 的玩家列表框并删除该项目。

我想知道这是否可行,如果不是很清楚,请见谅!

foreach (var listBoxItem in listBox1.Items)
{
    // use the currently iterated list box item

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

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

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

编辑:

如果您想获取球员姓名,然后将其添加到 "Online Players" 列表框中或从中删除,您可以使用 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”应该仍然可以正常工作。希望这有帮助。