Taking multiple substrings from a single line in a .txt file in C#









up vote
1
down vote

favorite












Part of a class assignment I'm working on requires me to take isolate certain parts of a "students" info from a .txt file such as last name, first name, and gpa. While I can get the last name to appear properly, going beyond that is iffy, first names will be cut off or go to the middle initial seemingly randomly. I need help with having consistent cut off points based on markers (apostrophes) in the lines.



this is a sample of the .txt file Students2 which is located in the program's Bin Debug



(LIST (LIST 'Abbott 'Ashley 'J ) '8697387888 'ajabbott@mail.usi.edu 2.3073320999676614 )
(LIST (LIST 'Abbott 'Bradley 'M ) 'NONE 'bmabbott@mail.usi.edu 3.1915725161177115 )
(LIST (LIST 'Abbott 'Ryan 'T ) '8698689793 'rtabbott@mail.usi.edu 3.448215586562192 )
(LIST (LIST 'Abel 'Heather 'M ) '8698689386 'hmabel@mail.usi.edu 3.2517764202656974 )


Hopefully what I meant by markers makes sense, and the info is fake if anyone wondered.



The following is my current code, some of which was provided in an example.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication61

class Program

static void Main(string args)


StreamReader inFile;
string inLine;

if (File.Exists("Students2.txt"))

try




using (StreamWriter Malachi = File.AppendText("Students2.txt"))

Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


inFile = new StreamReader("Students2.txt");
while ((inLine = inFile.ReadLine()) != null)

int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
int endTwo = inLine.IndexOf(" ");
string firstname = inLine.Substring(end, endTwo);
int endThree = inLine.IndexOf(" ");
string email = inLine.Substring(endTwo, endThree);
Console.WriteLine( lastname + firstname );






catch (System.IO.IOException exc)

Console.WriteLine("Error");







Again I'd like to know what I'm doing wrong in regards to cutting off at specific points. Any and all help is greatly appreciated.










share|improve this question

















  • 2




    why not just split the string based on ' and use array index to get values?
    – Karthik Ganesan
    Nov 9 at 19:27










  • It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
    – elgonzo
    Nov 9 at 19:31











  • It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
    – Flydog57
    Nov 9 at 19:36














up vote
1
down vote

favorite












Part of a class assignment I'm working on requires me to take isolate certain parts of a "students" info from a .txt file such as last name, first name, and gpa. While I can get the last name to appear properly, going beyond that is iffy, first names will be cut off or go to the middle initial seemingly randomly. I need help with having consistent cut off points based on markers (apostrophes) in the lines.



this is a sample of the .txt file Students2 which is located in the program's Bin Debug



(LIST (LIST 'Abbott 'Ashley 'J ) '8697387888 'ajabbott@mail.usi.edu 2.3073320999676614 )
(LIST (LIST 'Abbott 'Bradley 'M ) 'NONE 'bmabbott@mail.usi.edu 3.1915725161177115 )
(LIST (LIST 'Abbott 'Ryan 'T ) '8698689793 'rtabbott@mail.usi.edu 3.448215586562192 )
(LIST (LIST 'Abel 'Heather 'M ) '8698689386 'hmabel@mail.usi.edu 3.2517764202656974 )


Hopefully what I meant by markers makes sense, and the info is fake if anyone wondered.



The following is my current code, some of which was provided in an example.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication61

class Program

static void Main(string args)


StreamReader inFile;
string inLine;

if (File.Exists("Students2.txt"))

try




using (StreamWriter Malachi = File.AppendText("Students2.txt"))

Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


inFile = new StreamReader("Students2.txt");
while ((inLine = inFile.ReadLine()) != null)

int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
int endTwo = inLine.IndexOf(" ");
string firstname = inLine.Substring(end, endTwo);
int endThree = inLine.IndexOf(" ");
string email = inLine.Substring(endTwo, endThree);
Console.WriteLine( lastname + firstname );






catch (System.IO.IOException exc)

Console.WriteLine("Error");







Again I'd like to know what I'm doing wrong in regards to cutting off at specific points. Any and all help is greatly appreciated.










share|improve this question

















  • 2




    why not just split the string based on ' and use array index to get values?
    – Karthik Ganesan
    Nov 9 at 19:27










  • It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
    – elgonzo
    Nov 9 at 19:31











  • It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
    – Flydog57
    Nov 9 at 19:36












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Part of a class assignment I'm working on requires me to take isolate certain parts of a "students" info from a .txt file such as last name, first name, and gpa. While I can get the last name to appear properly, going beyond that is iffy, first names will be cut off or go to the middle initial seemingly randomly. I need help with having consistent cut off points based on markers (apostrophes) in the lines.



this is a sample of the .txt file Students2 which is located in the program's Bin Debug



(LIST (LIST 'Abbott 'Ashley 'J ) '8697387888 'ajabbott@mail.usi.edu 2.3073320999676614 )
(LIST (LIST 'Abbott 'Bradley 'M ) 'NONE 'bmabbott@mail.usi.edu 3.1915725161177115 )
(LIST (LIST 'Abbott 'Ryan 'T ) '8698689793 'rtabbott@mail.usi.edu 3.448215586562192 )
(LIST (LIST 'Abel 'Heather 'M ) '8698689386 'hmabel@mail.usi.edu 3.2517764202656974 )


Hopefully what I meant by markers makes sense, and the info is fake if anyone wondered.



The following is my current code, some of which was provided in an example.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication61

class Program

static void Main(string args)


StreamReader inFile;
string inLine;

if (File.Exists("Students2.txt"))

try




using (StreamWriter Malachi = File.AppendText("Students2.txt"))

Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


inFile = new StreamReader("Students2.txt");
while ((inLine = inFile.ReadLine()) != null)

int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
int endTwo = inLine.IndexOf(" ");
string firstname = inLine.Substring(end, endTwo);
int endThree = inLine.IndexOf(" ");
string email = inLine.Substring(endTwo, endThree);
Console.WriteLine( lastname + firstname );






catch (System.IO.IOException exc)

Console.WriteLine("Error");







Again I'd like to know what I'm doing wrong in regards to cutting off at specific points. Any and all help is greatly appreciated.










share|improve this question













Part of a class assignment I'm working on requires me to take isolate certain parts of a "students" info from a .txt file such as last name, first name, and gpa. While I can get the last name to appear properly, going beyond that is iffy, first names will be cut off or go to the middle initial seemingly randomly. I need help with having consistent cut off points based on markers (apostrophes) in the lines.



this is a sample of the .txt file Students2 which is located in the program's Bin Debug



(LIST (LIST 'Abbott 'Ashley 'J ) '8697387888 'ajabbott@mail.usi.edu 2.3073320999676614 )
(LIST (LIST 'Abbott 'Bradley 'M ) 'NONE 'bmabbott@mail.usi.edu 3.1915725161177115 )
(LIST (LIST 'Abbott 'Ryan 'T ) '8698689793 'rtabbott@mail.usi.edu 3.448215586562192 )
(LIST (LIST 'Abel 'Heather 'M ) '8698689386 'hmabel@mail.usi.edu 3.2517764202656974 )


Hopefully what I meant by markers makes sense, and the info is fake if anyone wondered.



The following is my current code, some of which was provided in an example.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication61

class Program

static void Main(string args)


StreamReader inFile;
string inLine;

if (File.Exists("Students2.txt"))

try




using (StreamWriter Malachi = File.AppendText("Students2.txt"))

Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


inFile = new StreamReader("Students2.txt");
while ((inLine = inFile.ReadLine()) != null)

int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
int endTwo = inLine.IndexOf(" ");
string firstname = inLine.Substring(end, endTwo);
int endThree = inLine.IndexOf(" ");
string email = inLine.Substring(endTwo, endThree);
Console.WriteLine( lastname + firstname );






catch (System.IO.IOException exc)

Console.WriteLine("Error");







Again I'd like to know what I'm doing wrong in regards to cutting off at specific points. Any and all help is greatly appreciated.







c# substring






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 19:23









W. Schmitt

113




113







  • 2




    why not just split the string based on ' and use array index to get values?
    – Karthik Ganesan
    Nov 9 at 19:27










  • It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
    – elgonzo
    Nov 9 at 19:31











  • It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
    – Flydog57
    Nov 9 at 19:36












  • 2




    why not just split the string based on ' and use array index to get values?
    – Karthik Ganesan
    Nov 9 at 19:27










  • It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
    – elgonzo
    Nov 9 at 19:31











  • It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
    – Flydog57
    Nov 9 at 19:36







2




2




why not just split the string based on ' and use array index to get values?
– Karthik Ganesan
Nov 9 at 19:27




why not just split the string based on ' and use array index to get values?
– Karthik Ganesan
Nov 9 at 19:27












It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
– elgonzo
Nov 9 at 19:31





It seems you think that inLine.IndexOf(" "); gives different results when you call it repeatedly on the same string/line. Why would you think so? Please verify your assumptions by reading the documentation of string.IndexOf() (docs.microsoft.com/en-gb/dotnet/api/…) and spend some time observing what your code does in the debugger (Learn to debug using Visual Studio)
– elgonzo
Nov 9 at 19:31













It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
– Flydog57
Nov 9 at 19:36




It's an interesting problem (particularly since I worked in Lisp for two years way back in the late 90s). I believe the nested lists precludes using regular expressions, and it will also make a simple "look for markers" strategy very complicated. I think you will need a simple recursive parser (that recognizes three types, Lists, strings (that start with apostrophes) and numbers (that don't).
– Flydog57
Nov 9 at 19:36












3 Answers
3






active

oldest

votes

















up vote
2
down vote













you can use the split function and replace function to get what you are looking for something like this



 string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )";

test = test.Replace(")","");

string abc = test.Split(new string "'" , StringSplitOptions.None);

Console.WriteLine("Last Name =" + abc[1]);
Console.WriteLine("First Name =" + abc[2]);
Console.WriteLine("Middle Initial =" + abc[3]);
Console.WriteLine("Email gpa =" + abc[abc.Length-1]);


UPDATE



In case there is no apostrophe for gpa you can get those values as below, just replace the last line with this



 Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);


Here is a fiddle for it https://dotnetfiddle.net/vJYLXW






share|improve this answer


















  • 2




    Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
    – Flydog57
    Nov 9 at 19:48










  • Unfortunatelly your code gives incorrect email address.
    – Pati
    Nov 9 at 20:17










  • @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
    – Karthik Ganesan
    Nov 9 at 20:22


















up vote
1
down vote













you need to update inLine every time you take off substring like -



int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
inLine = inLine.Substring(end + 1);
int endTwo = inLine.IndexOf(" ");
string firstname = inLine.Substring(end, endTwo);
inLine = inLine.Substring(endTwo + 1);
int endThree = inLine.IndexOf(" ");
string email = inLine.Substring(endTwo, endThree);
.
.




and so on.....



OR you take find next space after end point of first string like this -



int start = inLine.IndexOf("'");
if (start >= 0)

inLine = inLine.Substring(start);
int end = inLine.IndexOf(" ");
string lastname = inLine.Substring(0, end);
int endTwo = inLine.IndexOf(' ', end + 1);
string firstname = inLine.Substring(end, endTwo - end);
int endThree = inLine.IndexOf(' ', endTwo + 1);
string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
endThree += 2; // to escape ')' after middle initial
int endFour = inLine.IndexOf(' ', endThree + 1);
string phone = inLine.Substring(endThree, endFour - endThree);
int endFive = inLine.IndexOf(' ', endFour + 1);
string email = inLine.Substring(endFour, endFive - endFour);
int endSix = inLine.IndexOf(' ', endFive + 1);
string gpa = inLine.Substring(endFive, endSix - endFive);
Console.WriteLine("Last Name - " + lastname);
Console.WriteLine("First Name - " + firstname);
Console.WriteLine("Middle Initial - " + middleinitial);
Console.WriteLine("Phone - " + phone);
Console.WriteLine("Email - " + email);
Console.WriteLine("GPA - " + gpa);



But I would recommend string split option. Read about String.Split Method



 while ((inLine = inFile.ReadLine()) != null)

var splits = inLine.Split(new ' ', ''' , StringSplitOptions.RemoveEmptyEntries);
if (splits.Length > 0)

Console.WriteLine("Last Name - " + splits[2]);
Console.WriteLine("First Name - " + splits[3]);
Console.WriteLine("Middle Initial - " + splits[4]);
Console.WriteLine("Phone - " + splits[6]);
Console.WriteLine("Email - " + splits[7]);
Console.WriteLine("GPA - " + splits[8]);


Console.WriteLine("------------------------------------------------------");






share|improve this answer






















  • Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
    – W. Schmitt
    Nov 9 at 20:30











  • In order to separate every element you need few adjustments. Check here.
    – Poonam Shingrut
    Nov 12 at 14:38










  • Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
    – Poonam Shingrut
    Nov 12 at 14:43

















up vote
0
down vote













It is much easier to get the desired fields with regexp:



Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

....

Match m = rPattern.Match(inLine );

if(m.Success)


string lastname = m.Groups[1].Value;
string firstname = m.Groups[2].Value;
string middlename = m.Groups[3].Value;
string phone = m.Groups[4].Value;
string email = m.Groups[5].Value;
string somenumber = m.Groups[6].Value;



Your code can be something like this:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
//PTK: don't forget include following line. IT'S IMPORTANT !!!
using System.Text.RegularExpressions;


namespace ConsoleApplication61

class Program

static void Main(string args)


StreamReader inFile;
string inLine;

//PTK: patern for your fields
Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

if (File.Exists("Students2.txt"))

try




using (StreamWriter Malachi = File.AppendText("Students2.txt"))

Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


inFile = new StreamReader("Students2.txt");
while ((inLine = inFile.ReadLine()) != null)


//PTK: match the current line with the pattern
Match m = rPattern.Match(inLine );


if(m.Success)


string lastname = m.Groups[1].Value;
string firstname = m.Groups[2].Value;
string middlename = m.Groups[3].Value;
string phone = m.Groups[4].Value;
string email = m.Groups[5].Value;
string somenumber = m.Groups[6].Value;

Console.WriteLine( lastname + firstname );







catch (System.IO.IOException exc)

Console.WriteLine("Error");







You can see working demo here: https://dotnetfiddle.net/FrU4l5






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232093%2ftaking-multiple-substrings-from-a-single-line-in-a-txt-file-in-c-sharp%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    you can use the split function and replace function to get what you are looking for something like this



     string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )";

    test = test.Replace(")","");

    string abc = test.Split(new string "'" , StringSplitOptions.None);

    Console.WriteLine("Last Name =" + abc[1]);
    Console.WriteLine("First Name =" + abc[2]);
    Console.WriteLine("Middle Initial =" + abc[3]);
    Console.WriteLine("Email gpa =" + abc[abc.Length-1]);


    UPDATE



    In case there is no apostrophe for gpa you can get those values as below, just replace the last line with this



     Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
    Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);


    Here is a fiddle for it https://dotnetfiddle.net/vJYLXW






    share|improve this answer


















    • 2




      Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
      – Flydog57
      Nov 9 at 19:48










    • Unfortunatelly your code gives incorrect email address.
      – Pati
      Nov 9 at 20:17










    • @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
      – Karthik Ganesan
      Nov 9 at 20:22















    up vote
    2
    down vote













    you can use the split function and replace function to get what you are looking for something like this



     string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )";

    test = test.Replace(")","");

    string abc = test.Split(new string "'" , StringSplitOptions.None);

    Console.WriteLine("Last Name =" + abc[1]);
    Console.WriteLine("First Name =" + abc[2]);
    Console.WriteLine("Middle Initial =" + abc[3]);
    Console.WriteLine("Email gpa =" + abc[abc.Length-1]);


    UPDATE



    In case there is no apostrophe for gpa you can get those values as below, just replace the last line with this



     Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
    Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);


    Here is a fiddle for it https://dotnetfiddle.net/vJYLXW






    share|improve this answer


















    • 2




      Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
      – Flydog57
      Nov 9 at 19:48










    • Unfortunatelly your code gives incorrect email address.
      – Pati
      Nov 9 at 20:17










    • @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
      – Karthik Ganesan
      Nov 9 at 20:22













    up vote
    2
    down vote










    up vote
    2
    down vote









    you can use the split function and replace function to get what you are looking for something like this



     string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )";

    test = test.Replace(")","");

    string abc = test.Split(new string "'" , StringSplitOptions.None);

    Console.WriteLine("Last Name =" + abc[1]);
    Console.WriteLine("First Name =" + abc[2]);
    Console.WriteLine("Middle Initial =" + abc[3]);
    Console.WriteLine("Email gpa =" + abc[abc.Length-1]);


    UPDATE



    In case there is no apostrophe for gpa you can get those values as below, just replace the last line with this



     Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
    Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);


    Here is a fiddle for it https://dotnetfiddle.net/vJYLXW






    share|improve this answer














    you can use the split function and replace function to get what you are looking for something like this



     string test = "(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )";

    test = test.Replace(")","");

    string abc = test.Split(new string "'" , StringSplitOptions.None);

    Console.WriteLine("Last Name =" + abc[1]);
    Console.WriteLine("First Name =" + abc[2]);
    Console.WriteLine("Middle Initial =" + abc[3]);
    Console.WriteLine("Email gpa =" + abc[abc.Length-1]);


    UPDATE



    In case there is no apostrophe for gpa you can get those values as below, just replace the last line with this



     Console.WriteLine("Email =" + (abc[abc.Length-1]).Split(' ')[0]);
    Console.WriteLine("gpa =" + (abc[abc.Length-1]).Split(' ')[1]);


    Here is a fiddle for it https://dotnetfiddle.net/vJYLXW







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 9 at 20:22

























    answered Nov 9 at 19:35









    Karthik Ganesan

    3,10611736




    3,10611736







    • 2




      Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
      – Flydog57
      Nov 9 at 19:48










    • Unfortunatelly your code gives incorrect email address.
      – Pati
      Nov 9 at 20:17










    • @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
      – Karthik Ganesan
      Nov 9 at 20:22













    • 2




      Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
      – Flydog57
      Nov 9 at 19:48










    • Unfortunatelly your code gives incorrect email address.
      – Pati
      Nov 9 at 20:17










    • @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
      – Karthik Ganesan
      Nov 9 at 20:22








    2




    2




    Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
    – Flydog57
    Nov 9 at 19:48




    Well, that's a lot simpler than what I was thinking about - but not as general. +1 to you!
    – Flydog57
    Nov 9 at 19:48












    Unfortunatelly your code gives incorrect email address.
    – Pati
    Nov 9 at 20:17




    Unfortunatelly your code gives incorrect email address.
    – Pati
    Nov 9 at 20:17












    @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
    – Karthik Ganesan
    Nov 9 at 20:22





    @Pati Your string doesn't have a apostrophe to differentiate between email and gpa if that's the case I have updated the fiddle accordingly
    – Karthik Ganesan
    Nov 9 at 20:22













    up vote
    1
    down vote













    you need to update inLine every time you take off substring like -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    inLine = inLine.Substring(end + 1);
    int endTwo = inLine.IndexOf(" ");
    string firstname = inLine.Substring(end, endTwo);
    inLine = inLine.Substring(endTwo + 1);
    int endThree = inLine.IndexOf(" ");
    string email = inLine.Substring(endTwo, endThree);
    .
    .




    and so on.....



    OR you take find next space after end point of first string like this -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    int endTwo = inLine.IndexOf(' ', end + 1);
    string firstname = inLine.Substring(end, endTwo - end);
    int endThree = inLine.IndexOf(' ', endTwo + 1);
    string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
    endThree += 2; // to escape ')' after middle initial
    int endFour = inLine.IndexOf(' ', endThree + 1);
    string phone = inLine.Substring(endThree, endFour - endThree);
    int endFive = inLine.IndexOf(' ', endFour + 1);
    string email = inLine.Substring(endFour, endFive - endFour);
    int endSix = inLine.IndexOf(' ', endFive + 1);
    string gpa = inLine.Substring(endFive, endSix - endFive);
    Console.WriteLine("Last Name - " + lastname);
    Console.WriteLine("First Name - " + firstname);
    Console.WriteLine("Middle Initial - " + middleinitial);
    Console.WriteLine("Phone - " + phone);
    Console.WriteLine("Email - " + email);
    Console.WriteLine("GPA - " + gpa);



    But I would recommend string split option. Read about String.Split Method



     while ((inLine = inFile.ReadLine()) != null)

    var splits = inLine.Split(new ' ', ''' , StringSplitOptions.RemoveEmptyEntries);
    if (splits.Length > 0)

    Console.WriteLine("Last Name - " + splits[2]);
    Console.WriteLine("First Name - " + splits[3]);
    Console.WriteLine("Middle Initial - " + splits[4]);
    Console.WriteLine("Phone - " + splits[6]);
    Console.WriteLine("Email - " + splits[7]);
    Console.WriteLine("GPA - " + splits[8]);


    Console.WriteLine("------------------------------------------------------");






    share|improve this answer






















    • Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
      – W. Schmitt
      Nov 9 at 20:30











    • In order to separate every element you need few adjustments. Check here.
      – Poonam Shingrut
      Nov 12 at 14:38










    • Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
      – Poonam Shingrut
      Nov 12 at 14:43














    up vote
    1
    down vote













    you need to update inLine every time you take off substring like -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    inLine = inLine.Substring(end + 1);
    int endTwo = inLine.IndexOf(" ");
    string firstname = inLine.Substring(end, endTwo);
    inLine = inLine.Substring(endTwo + 1);
    int endThree = inLine.IndexOf(" ");
    string email = inLine.Substring(endTwo, endThree);
    .
    .




    and so on.....



    OR you take find next space after end point of first string like this -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    int endTwo = inLine.IndexOf(' ', end + 1);
    string firstname = inLine.Substring(end, endTwo - end);
    int endThree = inLine.IndexOf(' ', endTwo + 1);
    string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
    endThree += 2; // to escape ')' after middle initial
    int endFour = inLine.IndexOf(' ', endThree + 1);
    string phone = inLine.Substring(endThree, endFour - endThree);
    int endFive = inLine.IndexOf(' ', endFour + 1);
    string email = inLine.Substring(endFour, endFive - endFour);
    int endSix = inLine.IndexOf(' ', endFive + 1);
    string gpa = inLine.Substring(endFive, endSix - endFive);
    Console.WriteLine("Last Name - " + lastname);
    Console.WriteLine("First Name - " + firstname);
    Console.WriteLine("Middle Initial - " + middleinitial);
    Console.WriteLine("Phone - " + phone);
    Console.WriteLine("Email - " + email);
    Console.WriteLine("GPA - " + gpa);



    But I would recommend string split option. Read about String.Split Method



     while ((inLine = inFile.ReadLine()) != null)

    var splits = inLine.Split(new ' ', ''' , StringSplitOptions.RemoveEmptyEntries);
    if (splits.Length > 0)

    Console.WriteLine("Last Name - " + splits[2]);
    Console.WriteLine("First Name - " + splits[3]);
    Console.WriteLine("Middle Initial - " + splits[4]);
    Console.WriteLine("Phone - " + splits[6]);
    Console.WriteLine("Email - " + splits[7]);
    Console.WriteLine("GPA - " + splits[8]);


    Console.WriteLine("------------------------------------------------------");






    share|improve this answer






















    • Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
      – W. Schmitt
      Nov 9 at 20:30











    • In order to separate every element you need few adjustments. Check here.
      – Poonam Shingrut
      Nov 12 at 14:38










    • Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
      – Poonam Shingrut
      Nov 12 at 14:43












    up vote
    1
    down vote










    up vote
    1
    down vote









    you need to update inLine every time you take off substring like -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    inLine = inLine.Substring(end + 1);
    int endTwo = inLine.IndexOf(" ");
    string firstname = inLine.Substring(end, endTwo);
    inLine = inLine.Substring(endTwo + 1);
    int endThree = inLine.IndexOf(" ");
    string email = inLine.Substring(endTwo, endThree);
    .
    .




    and so on.....



    OR you take find next space after end point of first string like this -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    int endTwo = inLine.IndexOf(' ', end + 1);
    string firstname = inLine.Substring(end, endTwo - end);
    int endThree = inLine.IndexOf(' ', endTwo + 1);
    string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
    endThree += 2; // to escape ')' after middle initial
    int endFour = inLine.IndexOf(' ', endThree + 1);
    string phone = inLine.Substring(endThree, endFour - endThree);
    int endFive = inLine.IndexOf(' ', endFour + 1);
    string email = inLine.Substring(endFour, endFive - endFour);
    int endSix = inLine.IndexOf(' ', endFive + 1);
    string gpa = inLine.Substring(endFive, endSix - endFive);
    Console.WriteLine("Last Name - " + lastname);
    Console.WriteLine("First Name - " + firstname);
    Console.WriteLine("Middle Initial - " + middleinitial);
    Console.WriteLine("Phone - " + phone);
    Console.WriteLine("Email - " + email);
    Console.WriteLine("GPA - " + gpa);



    But I would recommend string split option. Read about String.Split Method



     while ((inLine = inFile.ReadLine()) != null)

    var splits = inLine.Split(new ' ', ''' , StringSplitOptions.RemoveEmptyEntries);
    if (splits.Length > 0)

    Console.WriteLine("Last Name - " + splits[2]);
    Console.WriteLine("First Name - " + splits[3]);
    Console.WriteLine("Middle Initial - " + splits[4]);
    Console.WriteLine("Phone - " + splits[6]);
    Console.WriteLine("Email - " + splits[7]);
    Console.WriteLine("GPA - " + splits[8]);


    Console.WriteLine("------------------------------------------------------");






    share|improve this answer














    you need to update inLine every time you take off substring like -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    inLine = inLine.Substring(end + 1);
    int endTwo = inLine.IndexOf(" ");
    string firstname = inLine.Substring(end, endTwo);
    inLine = inLine.Substring(endTwo + 1);
    int endThree = inLine.IndexOf(" ");
    string email = inLine.Substring(endTwo, endThree);
    .
    .




    and so on.....



    OR you take find next space after end point of first string like this -



    int start = inLine.IndexOf("'");
    if (start >= 0)

    inLine = inLine.Substring(start);
    int end = inLine.IndexOf(" ");
    string lastname = inLine.Substring(0, end);
    int endTwo = inLine.IndexOf(' ', end + 1);
    string firstname = inLine.Substring(end, endTwo - end);
    int endThree = inLine.IndexOf(' ', endTwo + 1);
    string middleinitial = inLine.Substring(endTwo, endThree - endTwo);
    endThree += 2; // to escape ')' after middle initial
    int endFour = inLine.IndexOf(' ', endThree + 1);
    string phone = inLine.Substring(endThree, endFour - endThree);
    int endFive = inLine.IndexOf(' ', endFour + 1);
    string email = inLine.Substring(endFour, endFive - endFour);
    int endSix = inLine.IndexOf(' ', endFive + 1);
    string gpa = inLine.Substring(endFive, endSix - endFive);
    Console.WriteLine("Last Name - " + lastname);
    Console.WriteLine("First Name - " + firstname);
    Console.WriteLine("Middle Initial - " + middleinitial);
    Console.WriteLine("Phone - " + phone);
    Console.WriteLine("Email - " + email);
    Console.WriteLine("GPA - " + gpa);



    But I would recommend string split option. Read about String.Split Method



     while ((inLine = inFile.ReadLine()) != null)

    var splits = inLine.Split(new ' ', ''' , StringSplitOptions.RemoveEmptyEntries);
    if (splits.Length > 0)

    Console.WriteLine("Last Name - " + splits[2]);
    Console.WriteLine("First Name - " + splits[3]);
    Console.WriteLine("Middle Initial - " + splits[4]);
    Console.WriteLine("Phone - " + splits[6]);
    Console.WriteLine("Email - " + splits[7]);
    Console.WriteLine("GPA - " + splits[8]);


    Console.WriteLine("------------------------------------------------------");







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 14:58

























    answered Nov 9 at 19:50









    Poonam Shingrut

    615




    615











    • Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
      – W. Schmitt
      Nov 9 at 20:30











    • In order to separate every element you need few adjustments. Check here.
      – Poonam Shingrut
      Nov 12 at 14:38










    • Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
      – Poonam Shingrut
      Nov 12 at 14:43
















    • Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
      – W. Schmitt
      Nov 9 at 20:30











    • In order to separate every element you need few adjustments. Check here.
      – Poonam Shingrut
      Nov 12 at 14:38










    • Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
      – Poonam Shingrut
      Nov 12 at 14:43















    Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
    – W. Schmitt
    Nov 9 at 20:30





    Using the second suggestion it does this: 'Abbott 'Ashley 'J ) ' 'J ) '8697387888 ) '8697387888 'ajab '8697387888 'ajabbott@mail.usi. At this point, I've tried to print the GPA but as you can see the middle initial repeats along with the phone number
    – W. Schmitt
    Nov 9 at 20:30













    In order to separate every element you need few adjustments. Check here.
    – Poonam Shingrut
    Nov 12 at 14:38




    In order to separate every element you need few adjustments. Check here.
    – Poonam Shingrut
    Nov 12 at 14:38












    Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
    – Poonam Shingrut
    Nov 12 at 14:43




    Also if you need to remove apostrophes from each element, you'll need further adjustments. String.split method is pretty straight forward and you can take out all the sub parts easily.
    – Poonam Shingrut
    Nov 12 at 14:43










    up vote
    0
    down vote













    It is much easier to get the desired fields with regexp:



    Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

    ....

    Match m = rPattern.Match(inLine );

    if(m.Success)


    string lastname = m.Groups[1].Value;
    string firstname = m.Groups[2].Value;
    string middlename = m.Groups[3].Value;
    string phone = m.Groups[4].Value;
    string email = m.Groups[5].Value;
    string somenumber = m.Groups[6].Value;



    Your code can be something like this:



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    //PTK: don't forget include following line. IT'S IMPORTANT !!!
    using System.Text.RegularExpressions;


    namespace ConsoleApplication61

    class Program

    static void Main(string args)


    StreamReader inFile;
    string inLine;

    //PTK: patern for your fields
    Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

    if (File.Exists("Students2.txt"))

    try




    using (StreamWriter Malachi = File.AppendText("Students2.txt"))

    Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


    inFile = new StreamReader("Students2.txt");
    while ((inLine = inFile.ReadLine()) != null)


    //PTK: match the current line with the pattern
    Match m = rPattern.Match(inLine );


    if(m.Success)


    string lastname = m.Groups[1].Value;
    string firstname = m.Groups[2].Value;
    string middlename = m.Groups[3].Value;
    string phone = m.Groups[4].Value;
    string email = m.Groups[5].Value;
    string somenumber = m.Groups[6].Value;

    Console.WriteLine( lastname + firstname );







    catch (System.IO.IOException exc)

    Console.WriteLine("Error");







    You can see working demo here: https://dotnetfiddle.net/FrU4l5






    share|improve this answer


























      up vote
      0
      down vote













      It is much easier to get the desired fields with regexp:



      Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

      ....

      Match m = rPattern.Match(inLine );

      if(m.Success)


      string lastname = m.Groups[1].Value;
      string firstname = m.Groups[2].Value;
      string middlename = m.Groups[3].Value;
      string phone = m.Groups[4].Value;
      string email = m.Groups[5].Value;
      string somenumber = m.Groups[6].Value;



      Your code can be something like this:



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.IO;
      //PTK: don't forget include following line. IT'S IMPORTANT !!!
      using System.Text.RegularExpressions;


      namespace ConsoleApplication61

      class Program

      static void Main(string args)


      StreamReader inFile;
      string inLine;

      //PTK: patern for your fields
      Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

      if (File.Exists("Students2.txt"))

      try




      using (StreamWriter Malachi = File.AppendText("Students2.txt"))

      Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


      inFile = new StreamReader("Students2.txt");
      while ((inLine = inFile.ReadLine()) != null)


      //PTK: match the current line with the pattern
      Match m = rPattern.Match(inLine );


      if(m.Success)


      string lastname = m.Groups[1].Value;
      string firstname = m.Groups[2].Value;
      string middlename = m.Groups[3].Value;
      string phone = m.Groups[4].Value;
      string email = m.Groups[5].Value;
      string somenumber = m.Groups[6].Value;

      Console.WriteLine( lastname + firstname );







      catch (System.IO.IOException exc)

      Console.WriteLine("Error");







      You can see working demo here: https://dotnetfiddle.net/FrU4l5






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        It is much easier to get the desired fields with regexp:



        Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

        ....

        Match m = rPattern.Match(inLine );

        if(m.Success)


        string lastname = m.Groups[1].Value;
        string firstname = m.Groups[2].Value;
        string middlename = m.Groups[3].Value;
        string phone = m.Groups[4].Value;
        string email = m.Groups[5].Value;
        string somenumber = m.Groups[6].Value;



        Your code can be something like this:



        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.IO;
        //PTK: don't forget include following line. IT'S IMPORTANT !!!
        using System.Text.RegularExpressions;


        namespace ConsoleApplication61

        class Program

        static void Main(string args)


        StreamReader inFile;
        string inLine;

        //PTK: patern for your fields
        Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

        if (File.Exists("Students2.txt"))

        try




        using (StreamWriter Malachi = File.AppendText("Students2.txt"))

        Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


        inFile = new StreamReader("Students2.txt");
        while ((inLine = inFile.ReadLine()) != null)


        //PTK: match the current line with the pattern
        Match m = rPattern.Match(inLine );


        if(m.Success)


        string lastname = m.Groups[1].Value;
        string firstname = m.Groups[2].Value;
        string middlename = m.Groups[3].Value;
        string phone = m.Groups[4].Value;
        string email = m.Groups[5].Value;
        string somenumber = m.Groups[6].Value;

        Console.WriteLine( lastname + firstname );







        catch (System.IO.IOException exc)

        Console.WriteLine("Error");







        You can see working demo here: https://dotnetfiddle.net/FrU4l5






        share|improve this answer














        It is much easier to get the desired fields with regexp:



        Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

        ....

        Match m = rPattern.Match(inLine );

        if(m.Success)


        string lastname = m.Groups[1].Value;
        string firstname = m.Groups[2].Value;
        string middlename = m.Groups[3].Value;
        string phone = m.Groups[4].Value;
        string email = m.Groups[5].Value;
        string somenumber = m.Groups[6].Value;



        Your code can be something like this:



        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using System.IO;
        //PTK: don't forget include following line. IT'S IMPORTANT !!!
        using System.Text.RegularExpressions;


        namespace ConsoleApplication61

        class Program

        static void Main(string args)


        StreamReader inFile;
        string inLine;

        //PTK: patern for your fields
        Regex rPattern= new Regex(@"'([^'s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+)['s]+([^'s]+)['s]+([^'s]+)['s]+([^'s]+)");

        if (File.Exists("Students2.txt"))

        try




        using (StreamWriter Malachi = File.AppendText("Students2.txt"))

        Malachi.WriteLine("(LIST (LIST 'Constant 'Malachi 'J ) '1832878847 'mconstant@mail.usi.edu 4.0000000000000000 )");


        inFile = new StreamReader("Students2.txt");
        while ((inLine = inFile.ReadLine()) != null)


        //PTK: match the current line with the pattern
        Match m = rPattern.Match(inLine );


        if(m.Success)


        string lastname = m.Groups[1].Value;
        string firstname = m.Groups[2].Value;
        string middlename = m.Groups[3].Value;
        string phone = m.Groups[4].Value;
        string email = m.Groups[5].Value;
        string somenumber = m.Groups[6].Value;

        Console.WriteLine( lastname + firstname );







        catch (System.IO.IOException exc)

        Console.WriteLine("Error");







        You can see working demo here: https://dotnetfiddle.net/FrU4l5







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 at 20:26

























        answered Nov 9 at 20:08









        Pati

        42748




        42748



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232093%2ftaking-multiple-substrings-from-a-single-line-in-a-txt-file-in-c-sharp%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo