fork(3) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. class Address
  8. {
  9. public int? Number { get; set; }
  10. public string Street { get; set; }
  11. }
  12.  
  13. public static void Main()
  14. {
  15. var strings = new List<string>() {
  16. "10 Downing street",
  17. "Birch Lane 7",
  18. "Palm creek 8 street",
  19. "84 Chancellor place",
  20. "Battle on the somme 56 "
  21. };
  22.  
  23. List<Address> result = new List<Address>();
  24. foreach (string str in strings)
  25. {
  26. Address addr = new Address();
  27. result.Add(addr);
  28. int num, numIndex = int.MinValue;
  29. string[] tokens = str.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
  30. for (int i = 0; i < tokens.Length; i++)
  31. {
  32. if (!addr.Number.HasValue && int.TryParse(tokens[i], out num))
  33. {
  34. addr.Number = num;
  35. numIndex = i;
  36. }
  37. }
  38. if (addr.Number.HasValue)
  39. {
  40. addr.Street = string.Join(" ", tokens.Where((s, i) => i != numIndex).ToArray());
  41. }
  42. else
  43. {
  44. addr.Street = str;
  45. }
  46. }
  47.  
  48. foreach(var addr in result)
  49. Console.WriteLine("Street:{0} Number:{1}", addr.Street, addr.Number);
  50. }
  51. }
Success #stdin #stdout 0.04s 34840KB
stdin
Standard input is empty
stdout
Street:Downing street Number:10
Street:Birch Lane Number:7
Street:Palm creek street Number:8
Street:Chancellor place Number:84
Street:Battle on the somme Number:56