最近看到乙個輸入字串或者位元組陣列解析成鍵值對的**,可能對大家有用,簡單的寫了一下。
當然,你可以用json.net去處理json型別的鍵值對,網上很多資料,就不多說,這裡主要說是類似於自定資料格式,類似這樣的字串:
string
data
="sdada=57.4,aaasd=1234,fdafdsa=3.2,fdsafdsa=3.515,fdsafd=-3.471,gfdasgfd=0.098,gfdsgf=148.926,測試1=99.999750,測試2=57.1";
當然可以用linq來解決。這裡用比較傳統的方法,直接上**吧
///
/// 鍵值對解析helper,修改matchkey作為鍵值之間的符號,matchvalue為鍵值對之間的符號
///public
static
class keyvaluehelper
public
static
string matchvalue
////// 解析輸入bytes中的鍵值對
//////
輸入位元組陣列
///解析後的鍵值對字典
public
static dictionary getconentbybytes(byte data)
////// 解析輸入字串中的鍵值對
//////
輸入字串
///解析後的鍵值對字典
public
static dictionary getconentbystring(string data)
;//predicatematchcomma = delegate(string value)
//;if (data.substring(data.length - 1) != matchvalue)
try}
}catch(exception ex)
return conents;}}
測試下:
class program
value : ",e.key
.tostring(),e.value
.tostring()));
}console.readkey();
}
執行結果:
收工。
鍵值對解析
鍵值對解析helper,修改matchkey作為鍵值之間的符號,matchvalue為鍵值對之間的符號 public static class keyvaluehelper public static string matchvalue 解析輸入bytes中的鍵值對 輸入位元組陣列 解析後的鍵值對字...
C 鍵值對類相關
一 c 鍵值對類有以下類 idictionaryidc new dictionary keyvaluepairpar keyvaluepair shoplistcombobox.selecteditem hashtable ht new hashtable file建立乙個hashtable例項 h...
C 鍵值對類相關
一 c 鍵值對類有以下類 idictionaryidc new dictionary keyvaluepairpar keyvaluepair shoplistcombobox.selecteditem hashtable ht new hashtable file建立乙個hashtable例項 h...