我循环遍历一个数组,我再次循环这些值以使其与字符串匹配,如果整个给定的字符串匹配将是数组的整个元素,我应该打印该字符串,但它不起作用
public class find
{
string[] inputs = {"Third Street Promenade, 1220 3rd St ,Santa Monica, CA 90401","Hi there should"};
string store_tran1 = "1220 3rd St ,Santa Monica, CA 90401"
foreach(var store in stores){
List<string> outputs = new List<string>();
foreach (var input in inputs)
{
outputs.AddRange((new String(input.ToCharArray()
.Where(c => Char.IsLetterOrDigit(c) || c == ' ')
.ToArray()).Split(' ')));
}
foreach (var output in outputs)
{
//string sto = store.ToString ();
//Console.Write (output+"\n");
bool store_match_check_1 = Regex.IsMatch (store_tran1, @""+Regex.Escape (output)+"", RegexOptions.IgnoreCase);
if (store_match_check_1 == true) {
//string store_match_check1 = store_match_check_1.Groups [1].Value;
res = true;
} else {
store_array_check.Add ("one");
res = false;
}
}
}
}
预期产出:
Third Street Promenade, 1220 3rd St ,Santa Monica, CA 9040
我怎么能得到这个woking
答案 0 :(得分:0)
为什么不
var inputs = new[] { "Third Street Promenade, 1220 3rd St ,Santa Monica, CA 90401", "Hi there should" };
var store_tran1 = "1220 3rd St ,Santa Monica, CA 90401";
foreach (var input in inputs)
{
if (input.Contains(store_tran1))
{
Console.WriteLine(input);
}
}
(如果您需要,可以通过调用ToLower()
或调用Trim()
来删除空格来忽略大小写)
答案 1 :(得分:0)
你的正则表达式是从输入字符串中删除逗号,因此你不会得到任何匹配。
如果你真的想要删除所有非字母数字字符+逗号,你可以像这样使用LINQ:
var inputs = new[] {"Third Street Promenade, 1220 3rd St ,Santa Monica, CA 90401","Hi there should"};
var store_tran1 = "1220 3rd St ,Santa Monica, CA 90401";
inputs.Select(s => Regex.Replace(s, @"^\s*|[^\w\s,]+|\s*$", ""))
.Where(s => s.Contains(store_tran1))
.ToList()
.ForEach(Console.WriteLine);
正则表达式还将从输入字符串的开头和结尾修剪空格。