C# 字符串模式匹配
我有 2 个 C# 字符串列表,显示已加入和离开特定游戏的玩家。我试图通过匹配两个列表并消除那些已离开游戏的人的条目来尝试确定谁仍在游戏中。请建议一个简单且无痛的算法来执行此操作。我当前的代码如下
string input = inputTextBox.Text;
string[] lines = input.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None);
List<string> filteredinput = new List<string>();
List<string> joinedlog = new List<string>();
List<string> leftlog = new List<string>();
for (int i = 0; i<lines.Length; i++)
{
if (lines[i].Contains("your game!"))
filteredinput.Add(lines[i]);
}
for (int i =0; i<filteredinput.Count; i++)
{
if (filteredinput[i].Contains("joined"))
joinedlog.Add(filteredinput[i]);
else if (filteredinput[i].Contains("left"))
leftlog.Add(filteredinput[i]);
}
这是一些示例输入:
{SheIsSoScrewed}[Ping:|] has joined your game!.
{AngeLa_Yoyo}[Ping:X] has joined your game!.
{SheIsSoScrewed} has left your game!(4).
I have 2 lists of strings in C# showing the players who have joined and left a particular game. I'm trying to attempt to determine who is still in the game by matching both lists and eliminating the entries of those who have left the game. Please suggest a easy and pain free algorithm to go about doing this. My current code is as follows
string input = inputTextBox.Text;
string[] lines = input.Split(new string[] {"\r\n", "\n"}, StringSplitOptions.None);
List<string> filteredinput = new List<string>();
List<string> joinedlog = new List<string>();
List<string> leftlog = new List<string>();
for (int i = 0; i<lines.Length; i++)
{
if (lines[i].Contains("your game!"))
filteredinput.Add(lines[i]);
}
for (int i =0; i<filteredinput.Count; i++)
{
if (filteredinput[i].Contains("joined"))
joinedlog.Add(filteredinput[i]);
else if (filteredinput[i].Contains("left"))
leftlog.Add(filteredinput[i]);
}
Here is some sample input :
{SheIsSoScrewed}[Ping:|] has joined your game!.
{AngeLa_Yoyo}[Ping:X] has joined your game!.
{SheIsSoScrewed} has left your game!(4).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您是在问如何获得您的两个名单,或者在您已经获得两个名单后如何找到当前的球员?
第二部分可以用 Linq 完成...
编辑为了回应您的评论,再次阅读您的问题后,显然上述内容将不起作用,因为您正在以一种奇怪的方式构建列表。
您将整个字符串存储在列表中,例如
user_1已离开游戏
,您可能应该做的只是存储用户名。如果你纠正了这个问题,那么上面的代码就可以完全满足你的要求。一个完整的例子:
Are you asking how to get your two lists, or how to find the current players after you've already got the two lists?
The second part can be done with Linq....
EDIT In response to your comment, having read your question again then obviously the above won't work because you are building your lists in a weird way.
You are storing the whole string in the list, e.g.
user_1 has left the game
, what you should probably be doing is just storing the user name. If you correct this then the above code does exactly what you want.A full example:
相交 和除了是你的朋友。
另外,如果这是列表的唯一目的,请考虑使用类似 HashSet 相反。
Intersect and Except are your friends.
Also, if this is the sole purpose of the lists, consider using something like HashSet instead.
使用 linq 和正则表达式:
use linq and regex:
首先,您需要提取玩家姓名,以便计算差异:
现在计算差异:
First you need to extract the player names so you can calculate the difference:
Now calculate the difference:
使用
List.Find()
怎么样?链接 1
此处链接 2
How about using
List.Find()
?Link 1 here
Link 2 here