我试图基于2个必须按顺序排列的关键字来过滤出一个数组,到目前为止,我已经做到了:

string[] matchedOne = Array.FindAll(converList, s => s.Contains(split[1]));

string[] matchedTwo = Array.FindAll(matchedOne, s => s.Contains(split[2]));

if (matchedTwo.Length == 0)
{
    Console.Clear();
    Console.WriteLine("Sorry, Your Conversion is invalid");
    Main();
}

会聚者=

盎司,克,28.0
盎司,假28.0-我为示例添加的假1
克,盎司,3.0-我添加的虚假示例
磅,盎司,16.0
磅,公斤,0.454
品脱,升0.568
英寸,厘米2.5
英里,英寸,63360.0

如果用户输入5盎司,克,则通过“ matchedOne”时会找到; “一次,克,28.0”和“盎司,假,28.0”。 而不是像现在这样的“磅,盎司,16.0”和“克,盎司,3.0”。

然后在“ matchedTwo”中,只会找到“ on,gram,28.0”而不是“ ng,gram,ounce,3.0”

-补充:我不能在“ system;”上使用任何东西。

===============>>#1 票数:0 已采纳

var regex = new Regex(
      string.Format("^.*{0}.*{1}.*$",
      Regex.Escape(split[1]), Regex.Escape(split[2])),
      RegexOptions.IgnoreCase
      | RegexOptions.Multiline
      | RegexOptions.CultureInvariant
      | RegexOptions.IgnorePatternWhitespace
      | RegexOptions.Compiled
    );

Match m = regex.Match(converlist);

它基本上只与split[1]split[2]之前的行匹配

或不使用正则表达式

string[] match = Array.FindAll(matchedOne, s => s.IndexOf(split[1])==-1?false: s.IndexOf(split[2], s.IndexOf(split[1])) != -1);

和有效的转换...

const string converlist = "ounce,gram,28.0\r\nounce,fake,28.0\r\ngram, ounce, 3.0\r\npound,ounce,16.0\r\npound,kilogram,0.454\r\npint,litre,0.568\r\ninch, centimetre,2.5\r\nmile,inch,63360.0\r\n";
var split = "5,ounce,gram".Split(new[] { ',' });
var list = converlist.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries).ToList();
var matches = list.FindAll(s =>  s.IndexOf(split[1])==-1?false: s.IndexOf(split[2], s.IndexOf(split[1])) != -1);
var conversionLine = matches[0];
Console.WriteLine(conversionLine);
var conversionFactor = decimal.Parse(conversionLine.Split(new[] { ',' })[2]);
var valueToConvert = decimal.Parse(split[0].Trim());
Console.WriteLine(string.Format("{0} {2}s is {1} {3}s", valueToConvert, conversionFactor * valueToConvert, split[1], split[2]));

===============>>#2 票数:0

上面的答案可以满足您的需求,但这也可以。

string[] s = { "ounce,gram", "gram,ounce", "pound,carret" };

foreach (string temp in s.Where(x => (x.IndexOf("ounce")>-1) && (x.IndexOf("ounce") < x.IndexOf("gram"))))
            Debug.WriteLine(temp);

  ask by Kennyist translate from so

未解决问题?本站智能推荐: