C# 合并多个 list<string>

本文介绍了一种使用C#合并两个字符串列表并去除重复项的方法。通过遍历第二个列表,并检查项是否已存在于第一个列表中来实现。如果不存在,则将该元素添加到第一个列表中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我想合并2个List<string>,而且要求去除重复的项

C# code
?
1
2
3
4
5
6
7
8
9
             List< string > str1 =  new  List< string >();
             List< string > str2 =  new  List< string >();
             str1.Add( "FUCK" );
             str1.Add( "lb" );
             str1.Add( "sorr" );
             str2.Add( "FUCK" );
             str2.Add( "12" );
 
             str1.AddRange(str2);


AddRange用来合并List<string>是没什么问题,问题是不能除去重复的项,后来我这样

C# code
?
1
2
3
4
5
6
7
8
9
10
11
             List< string > str1 =  new  List< string >();
             List< string > str2 =  new  List< string >();
             str1.Add( "FUCK" );
             str1.Add( "lb" );
             str1.Add( "sorr" );
             str2.Add( "FUCK" );
             str2.Add( "12" );
 
             for  ( int  i = 0; i < str2.Count; i++)
                 if  (!str1.Contains(str2[i]))
                     str1.Add(str2[i]);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值