两个字符串数组交集(忽略大小写) [英] Intersection of two string array (ignore case)

查看:184
本文介绍了两个字符串数组交集(忽略大小写)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

 的String [] ARRAY1 = {红,蓝,绿,黑};
字符串[] ARRAY2 = {蓝,黄,黑};
 

我只需要匹配的字符串在一个阵列中(不区分大小写)。

结果应该是:

 的String []的结果= {蓝,黑}或{蓝,黑};
 

解决方案

怎么样的 Enumerable.Intersect 和<一href="http://msdn.microsoft.com/en-us/library/system.stringcomparer_properties.aspx"><$c$c>StringComparer组合:

  //其他选项包括StringComparer.CurrentCultureIgnoreCase
//或StringComparer.InvariantCultureIgnoreCase
VAR的结果= array1.Intersect(ARRAY2,StringComparer.OrdinalIgnoreCase);
 

I have two arrays:

string[] array1 = { "Red", "blue", "green", "black" };
string[] array2 = { "BlUe", "yellow", "black" };

I need only the matching strings in one array (ignoring case).

Result should be:

string[] result = { "blue", "black" } or { "BlUe", "black" };

解决方案

How about an Enumerable.Intersect and StringComparer combo:

// other options include StringComparer.CurrentCultureIgnoreCase
// or StringComparer.InvariantCultureIgnoreCase
var results = array1.Intersect(array2, StringComparer.OrdinalIgnoreCase);

这篇关于两个字符串数组交集(忽略大小写)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆