在线等——这两个字符串怎么合并

消失的尘芥 2011-08-10 11:54:21
合并两个字符串(1)"4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
(2)"4,56,3,99"
若字符串(2)中出现的数字在字符串(1)中则删除,若没有就合并。
合并后的字符串是:67,54,98,99

可以将这两组字符串可以转换为两个数组。

...全文
253 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
老毕 2011-08-10
  • 打赏
  • 举报
回复
想了想,就是 (并集-交集) 的操作啊,之前想复杂了

List<string> result = seq_l.Union(seq_r)
.Except(seq_l.Intersect(seq_r))
.ToList<string>();
threenewbee 2011-08-10
  • 打赏
  • 举报
回复
假设 s1 是一个圆圈, s2 是一个圆圈,lz想要的是两个圆圈相交,不重叠的部分。

也就是 (s1 和 s2 的并集) - (s1 和 s2 的交集)。
交:Intersect
并:Union
减:Except
q107770540 2011-08-10
  • 打赏
  • 举报
回复

void Main()
{
string s1 = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string s2 = "4,56,3,99";
var temp=s1.Split(',').Select(s=>s.Trim()).Distinct();
string result=string.Join(",",temp.Except(s2.Split(','))
.Union(s2.Split(',').Except(temp)).ToArray());
Console.WriteLine(result); //67,54,98,99
}
threenewbee 2011-08-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s1 = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string s2 = "4,56,3,99";
var set1 = s1.Split(',').Select(x => x.Trim());
var set2 = s2.Split(',').Select(x => x.Trim());
var result = string.Join(",", set1.Union(set2).Except(set1.Intersect(set2)));
Console.WriteLine(result);
}
}
}
潘少博 2011-08-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxr0323 的回复:]
简单点儿的 list取差集


C# code
string lhs = "4,3,3,56,67,4,54,56,98,56";
string rhs = "4,56,3,99";
List<string> lt1 = lhs.Split(',').ToList().Distinct().ToList();
List<string> ……
[/Quote]
这个挺好。
代码在于看懂,不在于看着乱。
子夜__ 2011-08-10
  • 打赏
  • 举报
回复
简单点儿的 list取差集

string lhs = "4,3,3,56,67,4,54,56,98,56";
string rhs = "4,56,3,99";
List<string> lt1 = lhs.Split(',').ToList().Distinct().ToList();
List<string> lt2 = rhs.Split(',').ToList().Distinct().ToList();
lt1.Except(lt2)
.Union(lt2.Except(lt1))
.ToList().ForEach(I => Response.Write(I.ToString() + "<br/>"));


67
54
98
99
消失的尘芥 2011-08-10
  • 打赏
  • 举报
回复
ありがとうございます
threenewbee 2011-08-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s1 = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string s2 = "4,56,3,99";
string result = string.Join(",", s1.Split(',').Select(x => x.Trim()).Where(x => !s2.Split(',').Select(y => y.Trim()).Any(y => y == x)).Union(s2.Split(',').Select(x => x.Trim()).Where(x => !s1.Split(',').Select(y => y.Trim()).Any(y => x == y))).Distinct());
Console.WriteLine(result);
}
}
}

67,54,98,99
Press any key to continue . . .
消失的尘芥 2011-08-10
  • 打赏
  • 举报
回复
谢谢楼上的前辈,99需要是因为出现的新编号中,在原来的编号没有的话就补充进去。有的话则删除。这是个需求。
老毕 2011-08-10
  • 打赏
  • 举报
回复
string lhs = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string rhs = "4,56,3,99";

List<string> seq_l = lhs.Split(new char[] { ' ', ',' }).Select(s => s.Trim()).ToList<string>();
List<string> seq_r = rhs.Split(new char[] { ' ', ',' }).Select(s => s.Trim()).ToList<string>();

List<string> result = seq_l.Concat(seq_r)
.GroupBy(s => s)
.Where(g => g.Count() == 1)
.Select(r => r.Key)
.ToList<string>();

foreach (string s in result)
{
Console.WriteLine(s);
}

结果:

67
54
98
99
threenewbee 2011-08-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s1 = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string s2 = "4,56,3,99";
string result = string.Join(",", s1.Split(',').Select(x => x.Trim()).Where(x => !s2.Split(',').Select(y => y.Trim()).Any(y => y == x)).Distinct());
Console.WriteLine(result);
}
}
}

67,54,98
Press any key to continue . . .

不清楚为什么要99。
threenewbee 2011-08-10
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s1 = "4, 3, 3, 56, 67, 4, 54, 56, 98, 56";
string s2 = "4,56,3,99";
string result = string.Join(", ", s1.Split(',').Select(x => x.Trim()).Where(x => !s2.Split(',').Select(y => y.Trim()).Any(y => y == x)).OrderBy(x => x).Distinct());
Console.WriteLine(result);
}
}
}


p.s. 结果中应该没有99。
b0172716 2011-08-10
  • 打赏
  • 举报
回复
都老牛了啊~~呵呵

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧