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. Web Development
  3. ASP.NET
  4. substrings...

substrings...

Scheduled Pinned Locked Moved ASP.NET
questionhelp
9 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.
  • L Offline
    L Offline
    lhahehal
    wrote on last edited by
    #1

    may i ask how do i get the last word out from the string using a substring ?? say the string would be ... "Hi how are you" and i would like to take the you out ... and use it ... ?? thanks for the help ...

    C A 2 Replies Last reply
    0
    • L lhahehal

      may i ask how do i get the last word out from the string using a substring ?? say the string would be ... "Hi how are you" and i would like to take the you out ... and use it ... ?? thanks for the help ...

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      You can use LastIndexOf(" ") on the string to find the index of the last space character. Then you can use Substring(index+1) to get the last word in the string. Does this help?


      Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

      L 1 Reply Last reply
      0
      • C Colin Angus Mackay

        You can use LastIndexOf(" ") on the string to find the index of the last space character. Then you can use Substring(index+1) to get the last word in the string. Does this help?


        Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

        L Offline
        L Offline
        lhahehal
        wrote on last edited by
        #3

        hmm the lastindexof("") i get but as for substring(index+1) ??? cant seem to get it ...

        C 1 Reply Last reply
        0
        • L lhahehal

          may i ask how do i get the last word out from the string using a substring ?? say the string would be ... "Hi how are you" and i would like to take the you out ... and use it ... ?? thanks for the help ...

          A Offline
          A Offline
          Alvaro Mendez
          wrote on last edited by
          #4

          string text = "Hi how are you";
          string lastWord = text.Substring(text.LastIndexOf(' ') + 1);

          Regards, Alvaro


          You know what they say about arguing over the Internet...

          J 1 Reply Last reply
          0
          • L lhahehal

            hmm the lastindexof("") i get but as for substring(index+1) ??? cant seem to get it ...

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            LastIndexOf(" ") returns the last index of the string, in this case a space. You supply this index to the Substring() method, you need to add the one in order to move the index on to the first character of the last word, otherwise your result string will start with a space.

            int index = myString.LastIndexOf(" ");
            string result = myString.Substring(index+1);

            Does this help?


            Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums

            1 Reply Last reply
            0
            • A Alvaro Mendez

              string text = "Hi how are you";
              string lastWord = text.Substring(text.LastIndexOf(' ') + 1);

              Regards, Alvaro


              You know what they say about arguing over the Internet...

              J Offline
              J Offline
              jamesbronw
              wrote on last edited by
              #6

              you should have double quotes not single string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);

              L A 2 Replies Last reply
              0
              • J jamesbronw

                you should have double quotes not single string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);

                L Offline
                L Offline
                lhahehal
                wrote on last edited by
                #7

                thanks ... i sorta got it working thanks to you guys ... :)

                1 Reply Last reply
                0
                • J jamesbronw

                  you should have double quotes not single string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);

                  A Offline
                  A Offline
                  Alvaro Mendez
                  wrote on last edited by
                  #8

                  jamesbronw wrote: you should have double quotes not single Really?[^] Regards, Alvaro


                  You know what they say about arguing over the Internet...

                  R 1 Reply Last reply
                  0
                  • A Alvaro Mendez

                    jamesbronw wrote: you should have double quotes not single Really?[^] Regards, Alvaro


                    You know what they say about arguing over the Internet...

                    R Offline
                    R Offline
                    RussellZero
                    wrote on last edited by
                    #9

                    You can use either, the method's overloaded, so one version takes a string ( " " ), another a char ( ' ' ). The char method in my opinion would be the right one to go for in this instance. Regards Russell

                    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