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. Reading a specific word/phrase from a line

Reading a specific word/phrase from a line

Scheduled Pinned Locked Moved C#
tutorialcsharphelp
7 Posts 3 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.
  • Z Offline
    Z Offline
    zoroyster
    wrote on last edited by
    #1

    Hi Not sure if this was already asked, but I'm trying to find a way to get a certain text (or phrase) from a line in notepad using C# example: (notepad) >Name: Joseph Smith >Address: Main Street I need to get name (Joseph Smith) and the address only, ignoring the "name:" and "address:" so far i only know how to read the entire line, but i have no idea how in the world i can isolate the desired text/phrase Hope you guys can help. Thanks

    P 1 Reply Last reply
    0
    • Z zoroyster

      Hi Not sure if this was already asked, but I'm trying to find a way to get a certain text (or phrase) from a line in notepad using C# example: (notepad) >Name: Joseph Smith >Address: Main Street I need to get name (Joseph Smith) and the address only, ignoring the "name:" and "address:" so far i only know how to read the entire line, but i have no idea how in the world i can isolate the desired text/phrase Hope you guys can help. Thanks

      P Offline
      P Offline
      Patrick Etc
      wrote on last edited by
      #2

      If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.

      ------------ Cheers, Patrick

      X Z 2 Replies Last reply
      0
      • P Patrick Etc

        If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.

        ------------ Cheers, Patrick

        X Offline
        X Offline
        xibeifeijian
        wrote on last edited by
        #3

        Why don't you try a xml file?

        :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

        P 1 Reply Last reply
        0
        • X xibeifeijian

          Why don't you try a xml file?

          :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

          P Offline
          P Offline
          Patrick Etc
          wrote on last edited by
          #4

          He specifically said he's getting a line from a text file in notepad and gave an example line. XML works nicely, yes, but unfortunately not everyone uses XML to store application information ;)

          ------------ Cheers, Patrick

          1 Reply Last reply
          0
          • P Patrick Etc

            If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here.

            ------------ Cheers, Patrick

            Z Offline
            Z Offline
            zoroyster
            wrote on last edited by
            #5

            Patrick Sears wrote:

            If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick

            Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again

            X P 2 Replies Last reply
            0
            • Z zoroyster

              Patrick Sears wrote:

              If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick

              Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again

              X Offline
              X Offline
              xibeifeijian
              wrote on last edited by
              #6

              Split isn't a static method.Use in in a object instance and take care of the case.

              :^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)

              1 Reply Last reply
              0
              • Z zoroyster

                Patrick Sears wrote:

                If you can rely on a standard line format, it's pretty easy: string line = "Name: Joseph Smith"; string[] parts = string.split(line, ":"); Then the parts[] array will contain the following strings: parts[0] = "Name" parts[1] = " Joseph Smith" Note the space before Joseph. You could use string.Trim() to get rid of that, or you can modify the split to use StringSplitOptions.RemoveEmptyEntries. I'm assuming you're using C#, here. ------------ Cheers, Patrick

                Hi Patrick! thanks for the help. Yes, im using C#, but i received this error "Error 1 'string' does not contain a definition for 'split'" for string[] parts = string**.split**(line, ":"); do i need to import something? thanks again

                P Offline
                P Offline
                Patrick Etc
                wrote on last edited by
                #7

                Sorry, xibeifeijian has it right - it's an instance method, I was just indicating what class it belongs to. Sorry about that :( Sometimes I'm not sure if people will understand I'm speaking in pseudocode; sort of depends on experience level which I don't know up front. Let me know if you need anymore help figuring out how to do the parsing!

                ------------ Cheers, Patrick

                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