Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. User: jcruz Name: Jules Last: Cruz Email: Some@email.com
  2. User: jdoe Name: John Last: Doe Email: Some@email.com
  3. User: pmartin Name: Pete Last: Martin Email: Some@email.com
  4. User: rrichard Name: Reed Last: Richard Email: Some@email.com
  5.  
  6. var contact = new Conctact {
  7. Name = fieldFromLine,
  8. Last= fieldFromLine,
  9. Email = fieldFromLine
  10. }
  11.  
  12. String archivo = ((FileDialog)sender).FileName;
  13.  
  14. using (TextReader sr = new StreamReader(archivo,Encoding.UTF8))
  15. {
  16. String line = String.Empty;
  17. while ((line = sr.ReadLine()) != null )
  18. {
  19. string[] result = Regex.Split(line,"User:");
  20. //How to get the other fields...
  21.  
  22.  
  23. }
  24.  
  25. }
  26.  
  27. var result =File.ReadLines(fileName)
  28. .Select(line => line.Split(new string[]{"User:", "Name:", "Last:", "Email:"}, StringSplitOptions.RemoveEmptyEntries))
  29. .Select(parts => new Conctact(){ Name = parts[1], Last = parts[2], Email = parts[3] })
  30. .ToArray();
  31.  
  32. public class contact
  33. {
  34. public string Name { get; set; }
  35. public string Lname { get; set; }
  36. public string Email { get; set; }
  37. }
  38. List<contact> contact = new List<contact>();
  39. private void split()
  40. {
  41. var lines = File.ReadAllLines(@"txt file address");
  42. foreach (var line in lines)
  43. {
  44. var splitline=line.Split(':');
  45. string name = splitline[2].Replace("Last", "");
  46. string lname = splitline[3].Replace("Email","");
  47. contact.Add(new contact { Name = name, Lname = lname, Email = splitline[4] });
  48. }
  49. }
  50.  
  51. enum State { InUser, InName, InLast, InEmail }
  52.  
  53. State currentState = State.InUser; // you start off with the 'cursor' in the "User" section
  54. StringBuilder sb = new StringBuilder(); // this holds the current string element
  55. foreach(Char c in entireTextFile) { // presumably using `StreamReader.Read()`
  56. switch( currentState ) {
  57. case InUser:
  58. switch( c ) {
  59. // state transition logic here
  60. }
  61. // append the character to the StringBuilder until you've identified and reached the next field, then save the sb value to the appropriat
  62. case InName:
  63. // and so on...
  64. }
  65. }
  66.  
  67. Contact c = new Contact();
  68. string () tokens = input.Split(":".ToCharArray());
  69.  
  70. if (tokens.Count < 5)
  71. return; // error
  72.  
  73. // now strip the last word from each token
  74. c.Name = tokens(2).Substring(0, tokens(2).LastIndexOf(" ".ToCharArray())).Trim();
  75. c.Last = tokens(3).Substring(0, tokens(3).LastIndexOf(" ".ToCharArray())).Trim();
  76. c.Email = tokens(4).Trim();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement