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. Is it true there is no text parsing in C#?

Is it true there is no text parsing in C#?

Scheduled Pinned Locked Moved C#
csharpalgorithmsjsonhelpquestion
11 Posts 5 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.
  • J Offline
    J Offline
    jblouir
    wrote on last edited by
    #1

    I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)

    C D P J 4 Replies Last reply
    0
    • J jblouir

      I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      jblouir wrote:

      they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.

      Written by a moron then. The string class in C# has similar methods to CString in C++, it's at least as good as that class. It can be difficult to learn regex, but that's a moot point. You don't have to use regex, and if you do need it, it's wonderful that it's there.

      jblouir wrote:

      All I want to do is store room descriptors in a text file with a dillimeter and a value.

      The string's split method will let you take a line and turn it into an array of strings, with each string being one of your values. string [] lines = File.ReadAllLines(@"c:\file.txt"); foreach (string line in lines) { string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n } } if (elements.Count > 1) { // process elements[1], which is WestHall in the first instance. } // etc. }

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      D 1 Reply Last reply
      0
      • J jblouir

        I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)

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

        jblouir wrote:

        I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.

        Anything for a beginner is a nightmare, until they learn it's really very easy! I have no idea where you read that, but stop.

        jblouir wrote:

        Is it possible to pull this data out in chunks using StreamReader?

        Sure. But, you don't have to use ReadToEnd. Just read a line of the file (StreamReader.ReadLine), split the line on your delimitter (String.Split) to get the string values in an array, then process the first item in the array. That will tell you how many more items there should b in the array and what to do with them. Read the next line in the file. Rinse and Repeat.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007

        1 Reply Last reply
        0
        • C Christian Graus

          jblouir wrote:

          they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare.

          Written by a moron then. The string class in C# has similar methods to CString in C++, it's at least as good as that class. It can be difficult to learn regex, but that's a moot point. You don't have to use regex, and if you do need it, it's wonderful that it's there.

          jblouir wrote:

          All I want to do is store room descriptors in a text file with a dillimeter and a value.

          The string's split method will let you take a line and turn it into an array of strings, with each string being one of your values. string [] lines = File.ReadAllLines(@"c:\file.txt"); foreach (string line in lines) { string [] elements = line.split(new char [] { ',' } ); if (elements.Count > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n } } if (elements.Count > 1) { // process elements[1], which is WestHall in the first instance. } // etc. }

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

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

          Damn, you beat me to it! Frickin' router problems!

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007

          C 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Damn, you beat me to it! Frickin' router problems!

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            ROTFL - you're usually the one who beats me :P

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • J jblouir

              I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              The other responses are correct, but have you considered using XML?

              J 1 Reply Last reply
              0
              • J jblouir

                I just read an article on parsing in c# and they said that unlike older languages, parsing in c# in simple terms, for a beginner, can be a nightmare. All I want to do is store room descriptors in a text file with a dillimeter and a value. - The first value is used to find the correct line to print. e.g 01 - The second part being the name of the room, or unless its the rooms look value, e.g 011 - The third part being the description of your initial impression before looking closer. Data in the text file... 01,WestHall,The room is filled with old pottery and cobwebs.: 011,Searching the room you notice a hole where a key might fit.: 02,EastHall,Several torches line the walls, and a chest rests upon a stone slab in the back.: 021,Searching the room you notice the flames on the torches are not moving, almost as if...: Is it possible to pull this data out in chunks using StreamReader? Unless... I just thought of something as I write this very moment. Can I say... ReadToEnd, this entire text file and say drop it in to a string variable. Then from their use some sort of character searching in the string? Thanks for your help ahead of time. :-)

                J Offline
                J Offline
                jblouir
                wrote on last edited by
                #7

                Thanks for your help previously, been implementing it into my code. Its changed a bit because I had problems with getting the .Count to work, doesnt appear to be in my namespaces. but anyway heres what I have, the program doesnt get any syntax errors, but during the program running I get an error that...(scroll down) #region dataload() // Extracting the data from databin and dumping it in to an array. static void dataload() { int intIndex = 0; string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything"; int intElement = 0; int i = 0; string[] lines = File.ReadAllLines("databin.txt"); foreach (string line in lines) { string[] elements = line.Split(new char[] { ',' }); intElement = elements.Length; if (intElement > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n intIndex = n; } } if (intElement > 1) { strRoom = elements[1]; } if (intElement > 2) { strDesc = elements[2]; } if (intElement > 3) { strLook = elements[3]; } RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.ArrRoomData.SetValue(descriptor,i); i = i++; // etc. } } #endregion the error points to GlobalVars.ArrRoomData.SetValue(descriptor,i); saying that the object reference not set to an instance of an object ArrRoomData is declared in a class called GlobalVars (Global Variables), where... private static object[] arrRoomData; public object[] ArrRoomData { get { return arrRoomData; } set { arrRoomData = value; } } I have a feeling that once it gets past this error im going to get another one on the fact that its going to try and declare another object named descriptor, which im guessing I can I can do an if check on a boolean to see if its already been declared once.

                C 1 Reply Last reply
                0
                • P PIEBALDconsult

                  The other responses are correct, but have you considered using XML?

                  J Offline
                  J Offline
                  jblouir
                  wrote on last edited by
                  #8

                  I am trying to start with basic stuff, instead of jumping into 2d/3d graphics and using databases, so im just making a text adventure (In console) with simple data files for storing data, I might use serialisation for saving the game state(save games). After that then ill move up to the fancier code. :o)

                  RaviBeeR 1 Reply Last reply
                  0
                  • J jblouir

                    Thanks for your help previously, been implementing it into my code. Its changed a bit because I had problems with getting the .Count to work, doesnt appear to be in my namespaces. but anyway heres what I have, the program doesnt get any syntax errors, but during the program running I get an error that...(scroll down) #region dataload() // Extracting the data from databin and dumping it in to an array. static void dataload() { int intIndex = 0; string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything"; int intElement = 0; int i = 0; string[] lines = File.ReadAllLines("databin.txt"); foreach (string line in lines) { string[] elements = line.Split(new char[] { ',' }); intElement = elements.Length; if (intElement > 0) { int n; if (int.TryParse(elements[0], out n)) { // the first element was a number, now stored in n intIndex = n; } } if (intElement > 1) { strRoom = elements[1]; } if (intElement > 2) { strDesc = elements[2]; } if (intElement > 3) { strLook = elements[3]; } RoomData descriptor = new RoomData(intIndex, strRoom, strDesc, strLook); GlobalVars.ArrRoomData.SetValue(descriptor,i); i = i++; // etc. } } #endregion the error points to GlobalVars.ArrRoomData.SetValue(descriptor,i); saying that the object reference not set to an instance of an object ArrRoomData is declared in a class called GlobalVars (Global Variables), where... private static object[] arrRoomData; public object[] ArrRoomData { get { return arrRoomData; } set { arrRoomData = value; } } I have a feeling that once it gets past this error im going to get another one on the fact that its going to try and declare another object named descriptor, which im guessing I can I can do an if check on a boolean to see if its already been declared once.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    It's perhaps intElement.Length

                    jblouir wrote:

                    string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything";

                    This is outside your loop, so if a row fails, instead of a default, it will continue to contain the last value found.

                    jblouir wrote:

                    saying that the object reference not set to an instance of an object

                    Odds are that a line doesn't have enough elements, and removing Count is making a check that is just invalid.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    J 1 Reply Last reply
                    0
                    • J jblouir

                      I am trying to start with basic stuff, instead of jumping into 2d/3d graphics and using databases, so im just making a text adventure (In console) with simple data files for storing data, I might use serialisation for saving the game state(save games). After that then ill move up to the fancier code. :o)

                      RaviBeeR Offline
                      RaviBeeR Offline
                      RaviBee
                      wrote on last edited by
                      #10

                      jblouir wrote:

                      so im just making a text adventure (In console)

                      Splendid!  Splendid!  Check out Collosal Cave Adventure[^] :cool: if you haven't already played it. /ravi

                      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        It's perhaps intElement.Length

                        jblouir wrote:

                        string strRoom = "anything"; string strDesc = "anything"; string strLook = "anything";

                        This is outside your loop, so if a row fails, instead of a default, it will continue to contain the last value found.

                        jblouir wrote:

                        saying that the object reference not set to an instance of an object

                        Odds are that a line doesn't have enough elements, and removing Count is making a check that is just invalid.

                        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                        J Offline
                        J Offline
                        jblouir
                        wrote on last edited by
                        #11

                        At the bottom of the code where the particular code of line is highlighted. descriptor is holding the correct values for index, room name, room desc, and room look. The .Count I got around by using .Length which returns the number of elements in that line which logically to me does the same thing.

                        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