fork download
  1. using System;
  2. using System.Globalization;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. string text = "2013-11-11 17:56:14 [INFO] $4/time: $5Changes the time on each $4world";
  11. var tagsCT = new[] { "$1","$2","$3","$4" };
  12.  
  13. var tagIndices = new Dictionary<string, IList<int>>();
  14. foreach (string tag in tagsCT)
  15. {
  16. IList<int> indices;
  17. if (tagIndices.TryGetValue(tag, out indices))
  18. continue; // to prevent the same indices on duplicate tags, you could also use a HashSet<string> instead of the array
  19. else
  20. {
  21. indices = new List<int>();
  22. tagIndices.Add(tag, indices);
  23. }
  24. int index = text.IndexOf(tag);
  25. while (index >= 0)
  26. {
  27. indices.Add(index);
  28. index = text.IndexOf(tag, index + 1);
  29. }
  30. }
  31.  
  32. foreach(var kv in tagIndices)
  33. Console.WriteLine("Tag: {0} Indices: {1}",
  34. kv.Key,
  35. String.Join(",",kv.Value.Select(i=>i.ToString()).ToArray()));
  36. }
  37. }
Success #stdin #stdout 0.06s 34200KB
stdin
Standard input is empty
stdout
Tag: $1 Indices: 
Tag: $2 Indices: 
Tag: $3 Indices: 
Tag: $4 Indices: 27,63