如何在 C# 中比较字符串和字符串数组?
How to compare string to String Array in C#?
我有一个字符串;
String uA = "Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 Twitter for iPhone";
String[] a= {"iphone","ipad","ipod"};
它必须 return ipad
因为 ipad 在 ipad 字符串的第一个匹配项中。
在其他情况下
String uA = "Mozilla/5.0 (iPhone/iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508";
相同的字符串数组首先匹配到 iPhone
。
试试这个:
String uA = "Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 Twitter for iPhone";
String[] a = { "iphone", "ipad", "ipod" };
var result = a.Select(i => new { item = i, index = uA.IndexOf(i) })
.Where(i=>i.index >= 0)
.OrderBy(i=>i.index)
.First()
.item;
所以您想要数组中目标字符串中最早出现的单词?听起来你可能想要这样的东西:
return array.Select(word => new { word, index = target.IndexOf(word) })
.Where(pair => pair.index != -1)
.OrderBy(pair => pair.index)
.Select(pair => pair.word)
.FirstOrDefault();
详细步骤:
- 将单词投影到 word/index 对序列中,其中索引是该单词在目标字符串中的索引
- 通过删除索引为 -1 的对(
string.IndexOf
returns -1 如果未找到,则省略目标字符串中未出现的词)
- 按索引排序,使最早的单词出现在第一对
- Select 每对中的单词,因为我们不再关心索引
- Return 第一个单词,或者
null
如果序列为空
这里有一个 none linq 方法可以做到这一点,
static string GetFirstMatch(String uA, String[] a)
{
int startMatchIndex = -1;
string firstMatch = "";
foreach (string s in a)
{
int index = uA.ToLower().IndexOf(s.ToLower());
if (index == -1)
continue;
else if (startMatchIndex == -1)
{
startMatchIndex = index;
firstMatch = s;
}
else if (startMatchIndex > index)
{
startMatchIndex = index;
firstMatch = s;
}
}
return firstMatch;
}
我有一个字符串;
String uA = "Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 Twitter for iPhone";
String[] a= {"iphone","ipad","ipod"};
它必须 return ipad
因为 ipad 在 ipad 字符串的第一个匹配项中。
在其他情况下
String uA = "Mozilla/5.0 (iPhone/iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508";
相同的字符串数组首先匹配到 iPhone
。
试试这个:
String uA = "Mozilla/5.0 (iPad; CPU OS 8_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12D508 Twitter for iPhone";
String[] a = { "iphone", "ipad", "ipod" };
var result = a.Select(i => new { item = i, index = uA.IndexOf(i) })
.Where(i=>i.index >= 0)
.OrderBy(i=>i.index)
.First()
.item;
所以您想要数组中目标字符串中最早出现的单词?听起来你可能想要这样的东西:
return array.Select(word => new { word, index = target.IndexOf(word) })
.Where(pair => pair.index != -1)
.OrderBy(pair => pair.index)
.Select(pair => pair.word)
.FirstOrDefault();
详细步骤:
- 将单词投影到 word/index 对序列中,其中索引是该单词在目标字符串中的索引
- 通过删除索引为 -1 的对(
string.IndexOf
returns -1 如果未找到,则省略目标字符串中未出现的词) - 按索引排序,使最早的单词出现在第一对
- Select 每对中的单词,因为我们不再关心索引
- Return 第一个单词,或者
null
如果序列为空
这里有一个 none linq 方法可以做到这一点,
static string GetFirstMatch(String uA, String[] a)
{
int startMatchIndex = -1;
string firstMatch = "";
foreach (string s in a)
{
int index = uA.ToLower().IndexOf(s.ToLower());
if (index == -1)
continue;
else if (startMatchIndex == -1)
{
startMatchIndex = index;
firstMatch = s;
}
else if (startMatchIndex > index)
{
startMatchIndex = index;
firstMatch = s;
}
}
return firstMatch;
}