Advertisement
stanevplamen

02.8a.09.Dictionary

Jun 14th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. class DictionaryFirst
  6. {
  7.     static void TextSearch(string wordToFind, Dictionary<string, string> before)
  8.     {
  9.         foreach (KeyValuePair<string, string> kvp in before)
  10.         {
  11.             if ((wordToFind == kvp.Key) == true)
  12.             {
  13.                 string answer = string.Format("{0} - {1}", kvp.Key, kvp.Value);
  14.                 Console.WriteLine(answer);
  15.                 Environment.Exit(0);
  16.             }
  17.         }
  18.         Console.WriteLine("The word can not be found.");
  19.     }
  20.  
  21.     static void Main()
  22.     {
  23.         string dictionary = ".NET - platform for applications from Microsoft\n" +
  24.             "CLR - managed execution environment for .NET\n" +
  25.             "namespace - hierarchical organization of classes";
  26.  
  27.         Dictionary<string, string> before = new Dictionary<string, string>();
  28.  
  29.         string pattern = @"(?<before>.+) - (?<after>.+)";
  30.         string input = dictionary;
  31.         MatchCollection matches = Regex.Matches(input, pattern);
  32.  
  33.         for (int i = 0; i < matches.Count; i++)
  34.         {
  35.             before.Add((matches[i].Groups["before"].ToString().Trim(' ')), matches[i].Groups["after"].ToString().Trim(' '));
  36.         }
  37.         string wordToFind = "namespace";
  38.         TextSearch(wordToFind, before);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement