Cerebrus

Parsing CSV Phonelist

Feb 6th, 2010
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. //Related to question at:
  2. //http://groups.google.com/group/dotnetdevelopment/browse_thread/thread/54f6395f4936d6c9
  3.  
  4. using System.Linq;
  5. using System.Text;
  6. using System.Collections.Generic;
  7.  
  8. namespace ConsoleApplication
  9. {
  10.   class PhoneParser
  11.   {
  12.     private const int phoneLength = 10;    // Length of a phone number.
  13.     private const int groupLength = 100;  // Max length of each group of phone numbers.
  14.     private static List<string> inputPhoneList;
  15.     private static int phoneCount;
  16.  
  17.     static void Main(string[] args)
  18.     {
  19.       inputPhoneList = args[0].Split(',').ToList();
  20.       phoneCount = inputPhoneList.Count();
  21.       int endIdx = phoneCount > groupLength ? groupLength : phoneCount;
  22.       List<string> phoneList = ParsePhoneList(0, endIdx);
  23.       phoneList.ForEach((s) => { Console.WriteLine(s + "\r\n"); });
  24.     }
  25.  
  26.     private static List<string> ParsePhoneList(int startIdx, int endIdx)
  27.     {
  28.       List<string> pList = new List<string>();
  29.       StringBuilder sb = new StringBuilder(groupLength * phoneLength);
  30.       int i = startIdx;
  31.       while (i < endIdx)
  32.       {
  33.         sb.Append(inputPhoneList[i]);
  34.         if (i < endIdx - 1)
  35.           sb.Append(",");
  36.         i++;
  37.       }
  38.       pList.Add(sb.ToString());
  39.       if (i < phoneCount)
  40.       {
  41.         endIdx += groupLength;
  42.         endIdx = phoneCount > endIdx ? endIdx : phoneCount;
  43.         pList.AddRange(ParsePhoneList(i, endIdx));
  44.       }
  45.       return pList;
  46.     }
  47.   }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment