Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- User: jcruz Name: Jules Last: Cruz Email: Some@email.com
- User: jdoe Name: John Last: Doe Email: Some@email.com
- User: pmartin Name: Pete Last: Martin Email: Some@email.com
- User: rrichard Name: Reed Last: Richard Email: Some@email.com
- var contact = new Conctact {
- Name = fieldFromLine,
- Last= fieldFromLine,
- Email = fieldFromLine
- }
- String archivo = ((FileDialog)sender).FileName;
- using (TextReader sr = new StreamReader(archivo,Encoding.UTF8))
- {
- String line = String.Empty;
- while ((line = sr.ReadLine()) != null )
- {
- string[] result = Regex.Split(line,"User:");
- //How to get the other fields...
- }
- }
- var result =File.ReadLines(fileName)
- .Select(line => line.Split(new string[]{"User:", "Name:", "Last:", "Email:"}, StringSplitOptions.RemoveEmptyEntries))
- .Select(parts => new Conctact(){ Name = parts[1], Last = parts[2], Email = parts[3] })
- .ToArray();
- public class contact
- {
- public string Name { get; set; }
- public string Lname { get; set; }
- public string Email { get; set; }
- }
- List<contact> contact = new List<contact>();
- private void split()
- {
- var lines = File.ReadAllLines(@"txt file address");
- foreach (var line in lines)
- {
- var splitline=line.Split(':');
- string name = splitline[2].Replace("Last", "");
- string lname = splitline[3].Replace("Email","");
- contact.Add(new contact { Name = name, Lname = lname, Email = splitline[4] });
- }
- }
- enum State { InUser, InName, InLast, InEmail }
- State currentState = State.InUser; // you start off with the 'cursor' in the "User" section
- StringBuilder sb = new StringBuilder(); // this holds the current string element
- foreach(Char c in entireTextFile) { // presumably using `StreamReader.Read()`
- switch( currentState ) {
- case InUser:
- switch( c ) {
- // state transition logic here
- }
- // append the character to the StringBuilder until you've identified and reached the next field, then save the sb value to the appropriat
- case InName:
- // and so on...
- }
- }
- Contact c = new Contact();
- string () tokens = input.Split(":".ToCharArray());
- if (tokens.Count < 5)
- return; // error
- // now strip the last word from each token
- c.Name = tokens(2).Substring(0, tokens(2).LastIndexOf(" ".ToCharArray())).Trim();
- c.Last = tokens(3).Substring(0, tokens(3).LastIndexOf(" ".ToCharArray())).Trim();
- c.Email = tokens(4).Trim();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement