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. Getting substring from a line

Getting substring from a line

Scheduled Pinned Locked Moved C#
tutorialcsharpdatabasehelplearning
7 Posts 4 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.
  • I Offline
    I Offline
    ipstefan
    wrote on last edited by
    #1

    So I read a file and that file has like 9k lines. the lines look like this(well it might not fit here, but is on a line only): A:"extatic" A:"extatici" A:"extaticul" A:"extaticului" A:"extaticii" A:"extaticilor" A:"extatică" A:"extatice" A:"extatica" A:"extaticei" A:"extaticele" A:"extaticelor" And I use textreader in c# to find a char in that text for example "extaticele" here. What I want is to extract if I found the char on that line the first char on that line. In this case "extatic" - but without "" of course and paste it in a text file. I dont know exactly how to work with find space so I want your help...cause I thought I'd find the first space in the line then take the index and with the index 0 of the line to extract "A:"extatic" ". Then remove the "A:" and the ". I saw somewhere an example with myString.find("") , but I can't find the "find" I might need a namespace.

    J S 2 Replies Last reply
    0
    • I ipstefan

      So I read a file and that file has like 9k lines. the lines look like this(well it might not fit here, but is on a line only): A:"extatic" A:"extatici" A:"extaticul" A:"extaticului" A:"extaticii" A:"extaticilor" A:"extatică" A:"extatice" A:"extatica" A:"extaticei" A:"extaticele" A:"extaticelor" And I use textreader in c# to find a char in that text for example "extaticele" here. What I want is to extract if I found the char on that line the first char on that line. In this case "extatic" - but without "" of course and paste it in a text file. I dont know exactly how to work with find space so I want your help...cause I thought I'd find the first space in the line then take the index and with the index 0 of the line to extract "A:"extatic" ". Then remove the "A:" and the ". I saw somewhere an example with myString.find("") , but I can't find the "find" I might need a namespace.

      J Offline
      J Offline
      Jason C Bourne
      wrote on last edited by
      #2

      In .Net, reference types are also objects. That means that most of the commands that you are looking for are present in the string itself. You can play with stuff like: String test = "abcd"; int index = test.IndexOf('b'); String test2 = test.Substring(index); // etc... There are plenty of methods on the string object. There are also static methods in the String class, like for example: String.Compare(string1, string2...) Last but not least, the namespace you are looking for is simply System.

      Jean-Christophe Grégoire

      I 1 Reply Last reply
      0
      • J Jason C Bourne

        In .Net, reference types are also objects. That means that most of the commands that you are looking for are present in the string itself. You can play with stuff like: String test = "abcd"; int index = test.IndexOf('b'); String test2 = test.Substring(index); // etc... There are plenty of methods on the string object. There are also static methods in the String class, like for example: String.Compare(string1, string2...) Last but not least, the namespace you are looking for is simply System.

        Jean-Christophe Grégoire

        I Offline
        I Offline
        ipstefan
        wrote on last edited by
        #3

        I use that namespace, but there is no mystring.Find - I just want to find the index of the first space in a line

        K 1 Reply Last reply
        0
        • I ipstefan

          I use that namespace, but there is no mystring.Find - I just want to find the index of the first space in a line

          K Offline
          K Offline
          Kristian Sixhoj
          wrote on last edited by
          #4

          ipstefan wrote:

          I use that namespace, but there is no mystring.Find - I just want to find the index of the first space in a line

          int index = myString.IndexOf(" ");

          Kristian Sixhoej

          I 1 Reply Last reply
          0
          • K Kristian Sixhoj

            ipstefan wrote:

            I use that namespace, but there is no mystring.Find - I just want to find the index of the first space in a line

            int index = myString.IndexOf(" ");

            Kristian Sixhoej

            I Offline
            I Offline
            ipstefan
            wrote on last edited by
            #5

            Yep I thought at that option too earlier and it works... I solved my problem with indexes and substrings. Thanks

            K 1 Reply Last reply
            0
            • I ipstefan

              Yep I thought at that option too earlier and it works... I solved my problem with indexes and substrings. Thanks

              K Offline
              K Offline
              Kristian Sixhoj
              wrote on last edited by
              #6

              You're welcome :)

              Kristian Sixhoej

              1 Reply Last reply
              0
              • I ipstefan

                So I read a file and that file has like 9k lines. the lines look like this(well it might not fit here, but is on a line only): A:"extatic" A:"extatici" A:"extaticul" A:"extaticului" A:"extaticii" A:"extaticilor" A:"extatică" A:"extatice" A:"extatica" A:"extaticei" A:"extaticele" A:"extaticelor" And I use textreader in c# to find a char in that text for example "extaticele" here. What I want is to extract if I found the char on that line the first char on that line. In this case "extatic" - but without "" of course and paste it in a text file. I dont know exactly how to work with find space so I want your help...cause I thought I'd find the first space in the line then take the index and with the index 0 of the line to extract "A:"extatic" ". Then remove the "A:" and the ". I saw somewhere an example with myString.find("") , but I can't find the "find" I might need a namespace.

                S Offline
                S Offline
                sourabhsorate
                wrote on last edited by
                #7

                hi ipstefan, my sugession is read contents of file sequentially and check whether the character u r reading is ", it it is then take all next characters untill next " apprears so u can get each character between "". From these chars u can make a string by using + operator and later it can be pasted where u want. Or as u mentioned myString.find() method, i hope find method is custom method not in framework. Instead, u can use following method of class String. String myString = "abcd"; String cutString = myString.Substring(0,3); the value of cutString will be 'abc' . Try to use this method. Ok.. Hope it will help u.

                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