2012-07-06 82 views
2

在我的字符串格式爲: "a = 23, b = 432, f = 321, gfs = 413, d = 42, k = 4242, t = 4314, (...etc...)" - 約30元搜索字符串集合

我需要有很多的字符串的相應的4個元素,e.g搜索的可能性:A,B,d,K。

例如查詢口頭:give me all string where a = 3, b, = 2, d = 31, k = 1

我應該使用什麼類型的集合?我應該創建一個班級嗎?有任何想法嗎?

+0

那麼上面的查詢會是什麼結果呢? – 2012-07-06 14:58:57

回答

1

如果你知道你的值是唯一的,我會構造一個哈希表,其中左邊的平等是你的關鍵,右邊是你的價值。這將幫助你避免任何串成形可能發生變化,例如額外的空格等

這裏是一些代碼示例

static void Main(string[] args) 
{ 
    string str = "a = 23, b = 432, f = 321, gfs = 413, d = 42, k = 4242, t = 4314"; 
    Dictionary<string,string> dictionary = ConstructDictionary(str); 
    // Now you can find what you want in your dictionary 
} 

private static Dictionary<string,string> ConstructDictionary(string str) 
{ 
    string[] pairs = str.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); // this will give you all the pairs X = Y 
    Dictionary<string, string> dictionary = new Dictionary<string, string>(); 
    foreach (string pair in pairs) 
    { 
     string[] keyValue = pair.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); // this will create an array of size 2 where 
     array[0] = key and array[1] = value; 
     string key = keyValue[0].Trim(); 
     string value = keyValue[1].Trim(); 
     if (!dictionary.ContainsKey(key)) 
     { 
      dictionary.Add(key, value); 
     } 
    } 
    return dictionary; 
} 
+0

你在建議他爲每個必須搜索的字符串創建一個字典嗎? – NominSim 2012-07-06 15:09:15

+0

不只是他想要搜索的那個,所以30個值 – Nir 2012-07-06 15:13:16

+0

OP想要通過_many_字符串搜索_particular_ values_並且希望所有滿足這些值的strings_。 – NominSim 2012-07-06 15:20:37

1

下面是使用輔助功能做的一種方式:

private static bool HasAll(string s, string[] keys, int[] vals) { 
    if (keys.Length != vals.Length) throw new ArgumentException("vals"); 
    var tt = s.Split(new[] {' ', ',', '='}); 
    for(var i = 0 ; i != keys.Length ; i++) { 
     var pos = Array.IndexOf(tt, keys[i])); 
     if (pos < 0 || pos == vals.Length-1 || !tt[i+1].Equals(vals[i].ToString())) { 
      return false; 
     } 
    } 
    return true; 
} 

現在你可以使用LINQ來獲得的項目是這樣的:

var keys = new[] {"a", "b", "d", "k"}; 
var vals = new[] {3, 2, 31, 1}; 
var res = data.Where(str => HasAll(str, keys, vals)).ToList(); 
0

東西LIK e這個查詢將起作用,但我會建議將你的值放入與字符串不同的數據結構中。可能是一個帶有元素名稱的結構,以便您可以查找多個值。

string s1 = "a = 32, b = 432, f = 321, gfs = 43, d = 42, k = 4, t = 44"; 
string s2 = "a = 23, b = 432, f = 321, gfs = 413, d = 42, k = 4242, t = 4314"; 
string s3 = "a = 23, b = 21, f = 321, gfs = 413, d = 42, k = 4242, t = 4314"; 
var array = new string[] { s1, s2, s3 }; 

var result = array.Where(s => s.Contains("f = 321") && s.Contains("b = 432")); 
1

如果我理解正確的quesiton,這應該這樣做

1)創建一個字典,其中的關鍵是滿弦和值是字符串

2)的切割片檢查條件與棋子的交集。交叉點的大小與標準大小相同,並且我們有一個匹配項。

[TestMethod] 
public void FindValuesInStrings() { 

    var strings = new[] { 
    "a = 23, b = 432, f = 321, gfs = 11, d = 42, k = 4242, t = 4314", //A 
    "a = 12, b = 123, f = 456, gfs = 11, d = 42, k = 4242, t = 4314", //B 
    "a = 11, b = 456, f = 789, gfs = 413, d = 42, k = 4242, t = 4314", //C 
    "a = 23, b = 789, f = 12, gfs = 13, d = 42, k = 4242, t = 4314", //D 
    }; 


    var dict = new Dictionary<string, IEnumerable<string>>(); 
    foreach (var str in strings) { 
     dict.Add(str, str.Split(',').Select(s => s.Trim())); 
    } 


    // finds the two entries where a = 23 (A & D) 
    var criteria = new[] { "a = 23" }; 
    var found = dict.Where(entry => 
     entry.Value.Intersect(criteria).Count() == criteria.Count()).Select(entry => entry.Key); 

    Assert.AreEqual(2, found.Count()); 

    // finds the single entry where a = 23 and gfs = 11 (A) 
    criteria = new[] { "a = 23", "gfs = 11" }; 
    found = dict.Where(entry => 
     entry.Value.Intersect(criteria).Count() == criteria.Count()).Select(entry => entry.Key); 

    Assert.AreEqual(1, found.Count()); 

}