Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Read from text file

C# Read from text file

Scheduled Pinned Locked Moved C#
csharpquestion
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Pavlex4
    wrote on last edited by
    #1

    This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:

    List questions = File.ReadLines(path)
    .Where(x => x.StartsWith("[") &&
    x.EndsWith("?"))
    .ToList();

            foreach (string x in questions)
            {
                listBox1.Items.Add(x);
            }
    
    M OriginalGriffO M L 4 Replies Last reply
    0
    • P Pavlex4

      This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:

      List questions = File.ReadLines(path)
      .Where(x => x.StartsWith("[") &&
      x.EndsWith("?"))
      .ToList();

              foreach (string x in questions)
              {
                  listBox1.Items.Add(x);
              }
      
      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      x does not endwith ? it contains ?

      1 Reply Last reply
      0
      • P Pavlex4

        This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:

        List questions = File.ReadLines(path)
        .Where(x => x.StartsWith("[") &&
        x.EndsWith("?"))
        .ToList();

                foreach (string x in questions)
                {
                    listBox1.Items.Add(x);
                }
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Look at each line, and use IndexOf and Substring:

        string s = @"[""Koje ostrvo od Italije razdvaja Ligursko more.?"",2,""Sicilija"",""Korzika"",""Sardinija"",""Kapri""]";
        int start = s.IndexOf('[') + 1;
        int end = s.IndexOf('?');
        string bitInTheMiddle = s.Substring(start, end - start);

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        P 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Look at each line, and use IndexOf and Substring:

          string s = @"[""Koje ostrvo od Italije razdvaja Ligursko more.?"",2,""Sicilija"",""Korzika"",""Sardinija"",""Kapri""]";
          int start = s.IndexOf('[') + 1;
          int end = s.IndexOf('?');
          string bitInTheMiddle = s.Substring(start, end - start);

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          P Offline
          P Offline
          Pavlex4
          wrote on last edited by
          #4

          How to do that for all lines?

          L 1 Reply Last reply
          0
          • P Pavlex4

            This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:

            List questions = File.ReadLines(path)
            .Where(x => x.StartsWith("[") &&
            x.EndsWith("?"))
            .ToList();

                    foreach (string x in questions)
                    {
                        listBox1.Items.Add(x);
                    }
            
            M Offline
            M Offline
            Midi_Mick
            wrote on last edited by
            #5

            StartsWith and EndsWith perform no operations on the strings themselves - they are simply boolean methods that return true or false based on what the string Starts or Ends with - so your code will return no results. Instead, you need to perform a "Select", and extract the part of the string you require. Two possible methods come to mind immediately:

            File.ReadLines(path).Select(x=>x.SubString(1, x.IndexOf("?")-1).ToList();

            or

            File.ReadLines(path).Select(x=>x.Split(new char[]{'[','?'}, StringSplitOption.RemoveEmptyEntries)[0]).ToList();

            You could also look at using Regex, but I think it might be a bit of overkill for this case.

            Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

            P 1 Reply Last reply
            0
            • P Pavlex4

              How to do that for all lines?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Use a loop. You really need to go and learn C# by studying a reference guide. You cannot learn it just by posting questions here.

              1 Reply Last reply
              0
              • M Midi_Mick

                StartsWith and EndsWith perform no operations on the strings themselves - they are simply boolean methods that return true or false based on what the string Starts or Ends with - so your code will return no results. Instead, you need to perform a "Select", and extract the part of the string you require. Two possible methods come to mind immediately:

                File.ReadLines(path).Select(x=>x.SubString(1, x.IndexOf("?")-1).ToList();

                or

                File.ReadLines(path).Select(x=>x.Split(new char[]{'[','?'}, StringSplitOption.RemoveEmptyEntries)[0]).ToList();

                You could also look at using Regex, but I think it might be a bit of overkill for this case.

                Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

                P Offline
                P Offline
                Pavlex4
                wrote on last edited by
                #7

                It's working:

                List questions = File.ReadLines(path)
                .Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
                foreach (var x in questions)
                {
                listBox2.Items.Add(x);
                }

                How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]

                M D 2 Replies Last reply
                0
                • P Pavlex4

                  It's working:

                  List questions = File.ReadLines(path)
                  .Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
                  foreach (var x in questions)
                  {
                  listBox2.Items.Add(x);
                  }

                  How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]

                  M Offline
                  M Offline
                  Midi_Mick
                  wrote on last edited by
                  #8

                  How about you give it a go using the techniques I just showed you? There is enough there to do that. Also, look up the methods I demonstrated on MSDN - that'll give you added info on how it all works. We're not here to do it all for you - hopefully pointing you in the right direction will have you learning.

                  Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

                  OriginalGriffO 1 Reply Last reply
                  0
                  • M Midi_Mick

                    How about you give it a go using the techniques I just showed you? There is enough there to do that. Also, look up the methods I demonstrated on MSDN - that'll give you added info on how it all works. We're not here to do it all for you - hopefully pointing you in the right direction will have you learning.

                    Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #9

                    Check out his previous questions - he doesn't seem to want to learn. :sigh:

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    1 Reply Last reply
                    0
                    • P Pavlex4

                      It's working:

                      List questions = File.ReadLines(path)
                      .Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
                      foreach (var x in questions)
                      {
                      listBox2.Items.Add(x);
                      }

                      How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Now that you've completely changed your requirement, it's time you started reading the documentation on these methods and started experimenting on your own on how they work. You're trying to learn by constantly asking questions that are easy to find the answers to and copying and pasting code you find on the internet. You're not making an effort to understand the code you're using. You have to start creating small programs that test the methods you're trying to use so you can see how they work. You also have to get to know how to use the debugger. It's there to debug YOU and better you're understanding of how your code works.

                      A guide to posting questions on CodeProject

                      Click this: Asking questions is a skill. Seriously, do it.
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • P Pavlex4

                        This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:

                        List questions = File.ReadLines(path)
                        .Where(x => x.StartsWith("[") &&
                        x.EndsWith("?"))
                        .ToList();

                                foreach (string x in questions)
                                {
                                    listBox1.Items.Add(x);
                                }
                        
                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        I think yours is an excellent example of "agile programming"; i.e. no planning, just "coding". It is obvious the leading and trailing "[]" can be eliminated. We then wind up with a "coma-delimited" file record; which can be split using string split (6 tokens). Then it's just a question of testing the first token for an "?". But, hey, as long as we come up with the "right answer", then it's all good. Right?

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups