开发者

How do I run through an array where the first array member is a string that needs to be split

I have an array {{".txt|.doc|.docx", "100000"}, {".xls|.xlsx|.xxx" , "10000"}, ".npp", "100000"}

I am trying to find a way to run a foreach loop on each member of the array in a loop while also checking the first member as a string not an array.

The code will search for all docs greater than 10000 bytes开发者_运维知识库, as long as the docs are txt, doc, docx, etc and then search all xls, and then npp. Some of the members of the first part of the array have multiple types, some just one. Is there an efficient way to do this?

I tried: if (size == (size[2,i]) && ((ext != sextension[1, 0].ToUpper())) && ((ext != sextension[1, x].ToUpper())))

but obviously that doesn't work. Sorry if this is not clear, but I am not sure how to explain it otherwise.


here is the code you put in the comment above:

c# string[,] extension = new string[5,2]{ 
  {".doc|.docx|.doc1","1000"} , 
  {".xls", "1000"} ,
  {".pdf","1000"},
  {".zip", "10000"}, 
  {".7z|.pz", "100000"} };

foreach (string p in array) 
{ 
  if (p == extension[0, 1]()) || ..ext2 = size or ..ext3 && (size) 
  {
     Console.WriteLine("Ext {0} has a file size equal to {1}", size_entention, size);

Does not make a whole lot of sense, but I will go for it (guessing all the way):

The best way to do the extension match given your data structure is to use regEx.Match(), however this can be slow. (Remember if your search string is .docx you will need to make it @"\.docx" before calling regEx.match() -- . is reserved in regEx.

I don't suggest this, it is slow. What you really want is a dictionary. Here is an example of using dictionary:

Dictionary<string, int> d = new Dictionary<string, int>();
d.Add(".doc", 1000);
d.Add(".docx", 1000);
d.Add(".doc1", 1000);
d.Add(".xls", 1000);
d.Add(".pdf", 1000);
d.Add(".zip", 10000);
d.Add(".7z", 100000);
d.Add(".pz", 100000);

foreach (string p in array) 
{ 
  if (d.ContainsKey(p)
    Console.WriteLine("Ext {0} has a file size equal to {1}",p, d[p]);
}

This will be much faster on the search that what you were doing. Setting up the dictionary is slow so I would suggest caching it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜