我用下面的函數和正則表達式來拆分數據從SharePoint返回查找數據時。
static private Dictionary<int,string> GetValues(string productsCellData)
{
// regular expression to split the data into an array, we need the ExplictCapture
// to prevent c# capturing the ;#
var regex = new Regex(@"((?<=\d);#|;#(?=\d))", RegexOptions.ExplicitCapture);
// our array of data that has been processed.
var productsCellsArray = regex.Split(productsCellData);
Dictionary<int, string> productsDictionary = new Dictionary<int, string>();
if (productsCellsArray.Length % 2 == 1)
{
// handle badly formatted string the array length should always be an even number.
}
// set local variables to hold the data in the loop.
int productKey = -1;
string productValue = string.Empty;
// loop over the array and create our dictionary.
for (var i = 0; i < productsCellsArray.Length; i++)
{
var item = productsCellsArray[i];
// process odd/even
switch (i % 2)
{
case 0:
productKey = Int32.Parse(item);
break;
case 1:
productValue = item;
if (productKey > 0)
{
productsDictionary.Add(productKey, productValue);
productKey = -1;
productValue = string.Empty;
}
break;
}
}
return productsDictionary;
}
這具有處理分隔符的優點;#如果它出現(不太可能,因爲它似乎)在值部分。
它還具有以下優點從ID
- 查找值
- 從字典
- 獲取編號的數組從字典中獲取值數組
- 如果存在檢查的值字典
- 檢查字典中是否存在編號
希望這有助於。
string [] nProductsCellData = Regex.Replace(ProductsCellData,@「\ d」,「;」)。Replace(「;#」,「」).Replace(「;;」,「;」)。Split ';'); (!產物= 「」) 的foreach(在nProductsCellData串產品) { 如果 { e.Row.Cells [I]。文本+ = 「